C - Typedef and function pointers

Started by
24 comments, last by Emmanuel Deloget 18 years, 1 month ago
Is there any way to typedef a function pointer. Such as... typedef (*fun_ptr)() funptr; funptr ExAmPlE = (*some_function);
Advertisement
Yes, but it's still ugly.
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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?
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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
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 }?

Beginner in Game Development?  Read here. And read here.

 

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