INTERVIEW QUESTIONS
C
FUNCTIONS IN C
DETAILS
Question: 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 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 * );
After you’ve gotten the declaration of pf, you can #include <string.h> and assign the address of strcmp() to pf: pf = strcmp;
|
Question:
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 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 * );
After you’ve gotten the declaration of pf, you can #include <string.h> and assign the address of strcmp() to pf: pf = strcmp; Source: CoolInterview.com
#include<stdio.h> #include<string.h> int (*pf)( const char *, const char * );
int main() { pf = strcmp; printf("%d",pf("baseer","asdf")); getch(); }
Here is how it should be called. printf("%d",pf("baseer","asdf"));
%d displays the return value Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date:
| Contact Syed Baseer Ahmed
calling function : sample(&d)
called function :
void sample(*d) Source: CoolInterview.com
Answered by: azmath | Date: 6/3/2010
| Contact azmath
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|