Pointer as return type of function

When any function is returning an address then the return data type of that function must be a pointer. Because returning value is an address, to store an address we need a pointer. In short, to hold an address we need a pointer so the function which returns the address needs a pointer variable to store it. The syntax for a function declaration, call, and definition is as written below.

Declaring :

<return data type>  *<function  name> (<data types of arguments>);


<pointer to store returned address>  = <function name>(<arguments>);

Defining :

<return data type>  *<function  name> (<arguments with data types>)

Example program to understand how to use function returning a pointer

In the below example, there is an int variable ‘a’ which is having value 1. There is a function named func and it is taking an int variable as an argument. Function func multiplies that int variable with 2 and returns the address of variable ‘a’.

To store the address of ‘a’, pointer ‘p’ is used and returned by the function. In the void main function, to store the address of returned variable we have used int type pointer ‘p’. By using ‘p’ and ‘*p’, we are printing the address of ‘a’ and the value of ‘a’.

int *fun(int a)
    a = a*2;
    int *p=&a;
    return p;

void main()
  int *p;
  int a=1;
  p = fun(a);
  printf("address of a = %u, value of a = %d ",p,*p);

Learning from this blog:

  1. Meaning of function returning pointer.
  2. Use of function returning pointer.
  3. Syntax of function calling, defining and declaring for a function returning a pointer.
  4. When to use a function returning a pointer.
  5. Code for function returning pointer.
Notify of
Inline Feedbacks
View all comments