Function returning a pointer

A function can also return a pointer to the calling program, the way it returns an int, a float or any other data type. To return a pointer, a function must explicitly mention in the calling program as well as in the function prototype. Let’s illustrate this with an example:

Write a program to illustrate a function returning a pointer.

/*Program that shows how a function returns a pointer */
# include<stdio.h>
void main( )
{
float *a;
float *func( ); /* function prototype */
a = func( );
printf ("Address = %u", a);
}
float *func( )
{
float r = 5.2;
return (&r);
}

OUTPUT

Address = 65516

This program only shows how a function can return a pointer. This concept will be used later while handling arrays.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: