Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How typedef works for function pointers

I think I may be suffering from the dreaded "accidental programmer" disease, at least when it comes to typedefs and function pointers. So I've been experimenting with all kinds of combinations involving these to analyse the results based on all the output I get.

But as I kept on trying different combinations, instead of analyzing the results I'm now just lost in process.

I'm hoping you guys will help me figure out this mess.

First code example

typedef void (print)(void);
void do_something (void) { printf("Hello World\n"); }

print *pr;
pr = &do_something;
pr(); // Hello World

Second code example

typedef void (print)(void);
void do_something (void) { printf("Hello World\n"); }

print *pr;
pr = do_something;
pr(); // Hello World

How do both the above code examples work, it's as if '&' has no effect on function name

third code example

typedef void (print)(void);
void do_something (void) { printf("Hello World\n"); }

print pr;
pr = do_something; // compile error
pr = &do_something; // compile error
pr();

I was hoping one of the above assignments to work here but damn! I really don't understand function pointers (and maybe typedef too).

like image 803
Raja Avatar asked Feb 20 '12 07:02

Raja


1 Answers

The address of a function name and the plain function name both mean the same thing, so & has no effect on a function name.

Similarly, when using function pointers, multiple dereferencing isn't a problem:

#include <stdio.h>
typedef void print(void);
static void dosomething(void) { printf("Hello World\n"); }

int main(void)
{
    print *f1 = dosomething;
    print *f2 = &dosomething;
    f2();
    (f1)();
    (*f1)();
    (**f2)();
    (***f1)();
    (****f2)();
    (*****f1)();
}

That compiles cleanly under:

gcc -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
    -Wold-style-definition -std=c99 xx.c -o xx

I would not claim that multiple stars is good style; it isn't. It is 'odd, and (yes, you may say it) perverse'. One is sufficient (and the one star is mainly for people like me who learned to program in C before the standard said "it is OK to call a function via a pointer without using the (*pointer_to_function)(arg1, arg2) notation; you can just write pointer_to_function(arg1, arg2) if you like"). Yes, it is weird. No, no other type (or class of types) exhibits the same behaviour, thank goodness.

like image 145
Jonathan Leffler Avatar answered Nov 15 '22 21:11

Jonathan Leffler