Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
What is the difference between Function pointer and pointer to
function and how to represent each of them?

//-------POINTING TO A FUNCTION---------
// function declaration
void FunctA();void main() {
// gives the declaration of a pointer (ptr)
// to FunctA()
int (*ptr)();// address of FunctA() assigned to ptr
ptr = FunctA;// calls the FunctA() function via the
// derefereced pointer ptr
(*ptr)();
}// function definition
void FunctA()
{
}
------RETURNING A POINTER FROM A FUNCTION----// *strn() will return a pointer that points
// to a character variable
char *strn();void main() {
printf("%s", strn());
}char *strn() {
char *ptr1 = "It's a string";// returns the Lvalue of the string
// (the address of 'I')
return(ptr1);
}
//--------PASSING A POINTER TO A FUNCTION---// tells the compiler that we will pass a
// pointer that points to a character
void letter(char *ch);void main() {
char *key = "a";
strn(key)
}void letter(char *ch) {
printf("my favorite letter is %c", ch);
}

Function pointer is synonymous to Pointer to function. It is just a variation in the name convention.
In the example quoted by GatorAid, a slight correction has to be made to the "Pointing to a Function".
If you want to create a Pointer to Function, say, void FunctA() in the example above, then the declaration for it ought to be:
void (*ptr)();
and not
int (*ptr)();
The return type for both has to be identical.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |