I cant get va_arg to pull in an argument of type pointer-to-function.
Submitted by: AdministratorTry using a typedef for the function pointer type.
The type-rewriting games which the va_arg macro typically plays are stymied by overly-complicated types such as pointer-to-function. To illustrate, a simplified implementation of va_arg is
#define va_arg(argp, type)
(*(type *)(((argp) += sizeof(type)) - sizeof(type)))
where argp's type (va_list) is char *. When you attempt to invoke
va_arg(argp, int (*)())
the expansion is
(*(int (*)() *)(((argp) += sizeof(int (*)())) - sizeof(int (*)())))
which is a syntax error (the first cast (int (*)() *) is meaningless).
If you use a typedef for the function pointer type, however, all will be well. Given
typedef int (*funcptr)();
the expansion of
va_arg(argp, funcptr)
is
(*(funcptr *)(((argp) += sizeof(funcptr)) - sizeof(funcptr)))
which works correctly.
Submitted by: Administrator
The type-rewriting games which the va_arg macro typically plays are stymied by overly-complicated types such as pointer-to-function. To illustrate, a simplified implementation of va_arg is
#define va_arg(argp, type)
(*(type *)(((argp) += sizeof(type)) - sizeof(type)))
where argp's type (va_list) is char *. When you attempt to invoke
va_arg(argp, int (*)())
the expansion is
(*(int (*)() *)(((argp) += sizeof(int (*)())) - sizeof(int (*)())))
which is a syntax error (the first cast (int (*)() *) is meaningless).
If you use a typedef for the function pointer type, however, all will be well. Given
typedef int (*funcptr)();
the expansion of
va_arg(argp, funcptr)
is
(*(funcptr *)(((argp) += sizeof(funcptr)) - sizeof(funcptr)))
which works correctly.
Submitted by: Administrator
Read Online C Programming Job Interview Questions And Answers
Top C Programming Questions
☺ | Why does this code crash? |
☺ | What is wrong with this declaration? |
☺ | How can I read/write structures from/to data files? |
☺ | What does static variable mean in c? |
☺ | How can I increase the allowable number of simultaneously open files? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |