CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How do you use a pointer to a function?
Category C Interview Questions
Rating (3.5) By 6 users
Added on 10/22/2004
Views 5330
Rate it!
Answers:

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;



#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



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
When would you use a pointer to a function?
View Answer
Why should we assign NULL to the elements (pointer) after freeing them?
View Answer
Is it better to use malloc() or calloc()?
View Answer
What is the difference between far and near?
View Answer
When should a far pointer be used?
View Answer
Can the size of an array be declared at runtime?
View Answer
What is the heap?
View Answer
What is the difference between NULL and NUL?
View Answer
What is a ?null pointer assignment? error? What are bus errors, memory faults, and core dumps?
View Answer
How can you determine the size of an allocated portion of memory?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1004/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.61