Jump to content
  • Advertisement
Sign in to follow this  
HughG

Typedef'ing function pointers

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quote:
typedef void (*SIG_ TYP)(int) ; / / from <signal.h> typedef void (*SIG_ ARG_ TYP)(int) ; SIG_ TYP signal(int, SIG_ ARG_ TYP) ; An array of pointers to functions is often useful. For example, the menu system for my mousebased editor is implemented using arrays of pointers to functions to represent operations. The system cannot be described in detail here, but this is the general idea: typedef void (*PF)() ; PF edit_ ops[] = { / / edit operations &cut, &paste, ©, &search }; PF file_ ops[] = { / / file management &open, &append, &close, &write };
I have only read about the typedef operator but it's basic format is typedef type newname; So how in the world does "typedef void (*PF)() work? What is considered the type and how is PF the newname?

Share this post


Link to post
Share on other sites
Advertisement
If you have a function of the type 'void func()' and you want to store the address of such a function in a variable, the type of the variable is 'void (*)()'. That is a pointer to a function without parameters and returning nothing.

When you introduce a new type using typedef, you place the name of the new type at the same position where you would normally place the name of a definition.

So 'typedef void(*PF)()' creates a new type which is a pointer to a function without parameters and returning nothing. You can then define a variable of that type

PF pf;

and assign a value, which is the address of a function

void func() {}
...
pf = func;

Share this post


Link to post
Share on other sites
Also, you call func() (the function you assigned to pf) like this:

pf();

So you treat the function pointer just like a normal function

Share this post


Link to post
Share on other sites
Quote:
Original post by HughG
I have only read about the typedef operator but it's basic format is

typedef type newname;


Actually the syntax of a typedef is more like this:
    typedef declaration; 
The "declaration" is like a normal declaration except the name of the new type goes in the place where the variable normally goes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!