How do you use a pointer to a function? Ans. 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 in this way: 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. If you don't include parentheses, as in int *pf( const char *, const char * ); /* wrong */ you'll get the same thing as this: (int *) pf( const char *, const char * ); /* wrong */ That is, you'll have a declaration of a function that returns int*. After you've gotten the declaration of pf, you can #include and assign the address of strcmp() to pf: pf = strcmp; or pf = & strcmp; /* redundant & */ You don't need to go indirect on pf to call it: if ( pf( str1, str2 ) > 0 ) /* ... */ - Study24x7
Social learning Network

Warning: include(./views/auth.php): failed to open stream: Permission denied in /var/www/html/live/loginRightSlider.php on line 18

Warning: include(): Failed opening './views/auth.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/live/loginRightSlider.php on line 18
06 Apr 2023 11:44 AM study24x7 study24x7

How do you use a pointer to a function? Ans. 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 in this way: int strcmp( const char *,...

See more

study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles