Computing.Net > Forums > Programming > Function pointer

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Function pointer

Reply to Message Icon

Name: suhail
Date: January 1, 2003 at 07:48:32 Pacific
OS: windows 9x
CPU/Ram: 32 mb
Comment:

What is the difference between Function pointer and pointer to
function and how to represent each of them?



Sponsored Link
Ads by Google

Response Number 1
Name: GatorAid
Date: January 1, 2003 at 21:59:16 Pacific
Reply:

//-------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);
}



0

Response Number 2
Name: pHi
Date: February 26, 2003 at 07:51:36 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Function pointer

function pointers www.computing.net/answers/programming/function-pointers/6502.html

c++ function pointers www.computing.net/answers/programming/c-function-pointers/9516.html

pointers to function www.computing.net/answers/programming/pointers-to-function/4973.html