C - Typedef and function pointers
Is there any way to typedef a function pointer. Such as...
typedef (*fun_ptr)() funptr;
funptr ExAmPlE = (*some_function);
Yes, but it's still ugly.
The name of the typedef goes where the name of the variable is when declaring a function pointer variable.
typedef void (*funptr)(void);void some_function(void);funptr p_some_function = &some_function;
The name of the typedef goes where the name of the variable is when declaring a function pointer variable.
funptr p_some_function = &some_function;
The address of operator isn't needed.
funptr p_some_function = some_function;
The function name supplies the address of the function.
It's a good coding habit; when you take the address of a pointer to member function, the ampersand is necessary.
What does a member function in C look like? I didn't know that C had such things. C++ sure, but C?
And your point is that I should ignore good coding habits just because the file has a .c extension? Next you're going to tell me that I shouldn't bother to put
in my C header files because C never defines __cplusplus, right?
#ifdef __cplusplusextern "C" {#endif
in my C header files because C never defines __cplusplus, right?
My point is that there are differences between C and C++ and what constitutes a good coding habit in C++ may not hold for C and vice versa.
Then choose a different example to pick on. It's still a good coding habit in C, where hungarian notation is much more common and a programmer with experience in either apps or system hungarian will be distracted by an assignment to a pointer type with a non-decorated symbol name not-preceeded by an ampersand. That is to say something like
will trigger a warning sign in the back of his head while
won't.
lpfFunctionPointer = some_function;
will trigger a warning sign in the back of his head while
lpfFunctionPointer = &some_function;
won't.
Quote:Original post by SiCrane
And your point is that I should ignore good coding habits just because the file has a .c extension? Next you're going to tell me that I shouldn't bother to put#ifdef __cplusplusextern "C" {#endif
in my C header files because C never defines __cplusplus, right?
Silly question, but...
I see the beginning { but where's the }?
Somewhere after your done declaring your functions and before the #endif of the inclusion guard you would put:
#ifdef __cplusplus}#endif
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement