c - Is this the correct way to use va_arg with pointer to function? -
in function this:
typedef double(*dfun)(double); void tab(double x, int n, ...) { va_list args; va_start(args, n); printf("%5.2lf \t", x); (int i=0; i<n; i++) { dfun tmp = va_arg(args, dfun); printf(" %5.2lf \t", tmp(x)); } va_end(args); } is wrong if pull arguments this:
double(*tmp)(double) = va_arg(args, double(*)(double)); i came upon this article suggest different approach if doesn't work:
q: can't va_arg pull in argument of type pointer-to-function.
a: try using typedef function pointer type. type-rewriting games va_arg macro typically plays stymied overly-complicated types such pointer-to-function.
in case works in both versions (gcc 5.2.1), that's why wondering 1 approach better other? there potential error when don't typedef pointer function first?
using pointer without typedef can indeed cause problems in cases.
standard explains type used in va_arg must written in way, adding suffix * create pointer type:
7.16.1.1 va_arg macro
- the parameter type shall type name specified such type of pointer object has specified type can obtained postfixing * type.
therefore have compatible code, when using function pointers, should use typedef, because * cannot added function pointer type suffix create pointer type, because syntax isn't valid:
(double)(*)(double) => (double)(*)(double)*
but can added typedef:
dfun => dfun*
Comments
Post a Comment