Explain the concept of a function returning a pointer with an example 6m Jun2007

By | July 11, 2014

Explain the concept of a function returning a pointer in C with an example 6m Jun2007

Function returning a pointer in C

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:

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 while handling arrays.

Leave a Reply