Sunday, July 12, 2009

What is the difference between function returning pointer,function pointer & pointer to function in C lang??

A function that returns a pointer returns a reference to a memory address. A function that returns a pointer should NOT define the pointer or it will disappear when the function goes out of scope. A lot of times the pointer is passed into the function as a parameter, modified and returned.





A prototype for a function that returns a pointer looks like:


aPtr * funcReturnsPtr( SomeType * aPtr);





A function pointer is a pointer that points to the address of a function. That variable is a pointer to a function.


Here's an example of a variable pointing to a function:


float result = pt2Func(a, b);

What is the difference between function returning pointer,function pointer %26amp; pointer to function in C lang??
function returning pointer returns pointer value.


function pointers are pointer variables having its value pointing to the address of any function.


pointers to function means that the arguments passed in the function are pointers.
Reply:char *Function1()


{


return "Hello";


}


char *Function2()


{


return "World";


}





char * ((*function_pointer)());





main()


{


function_pointer = Function1;


printf("%s ", (*function_pointer)());


function_pointer = Function2;


printf("%s\n", (*function_pointer)());


}








-----------------------


In the above example Function1 and Function2 return pointers. function_pointer is a pointer to a function that returns a character. The output of the above program is


Hello World.





A function pointer is a pointer to a function.


No comments:

Post a Comment