1.

How Do You Use A Pointer To A Function?

Answer»

The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to CREATE a pointer, pf, that points to the strcmp() function. The strcmp() function is declared as SHOWN below
int strcmp( const char *, const char * )
To set up “pf” to point to the strcmp() function, you want a DECLARATION that LOOKS just like the strcmp() function's declaration, but that has *pf rather than strcmp:
int (*pf)( const char *, const char * );
Notice that you need to PUT parentheses around *pf.

The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared as shown below
int strcmp( const char *, const char * )
To set up “pf” to point to the strcmp() function, you want a declaration that looks just like the strcmp() function's declaration, but that has *pf rather than strcmp:
int (*pf)( const char *, const char * );
Notice that you need to put parentheses around *pf.



Discussion

No Comment Found