VOOZH about

URL: https://www.geeksforgeeks.org/c/how-to-return-a-pointer-from-a-function-in-c/

⇱ How to return a Pointer from a Function in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to return a Pointer from a Function in C

Last Updated : 6 Aug, 2025

Pointers in C programming language is a variable which is used to store the memory address of another variable. We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

Program 1: The below program will give segmentation fault since 'A' was local to the function: Output: Below is the output of the above program: 👁 Image
Explanation:

The main reason behind this scenario is that compiler always make a stack for a function call. As soon as the function exits the function stack also gets removed which causes the local variables of functions goes out of scope.

Static Variables have a property of preserving their value even after they are out of their scope. So to execute the concept of returning a pointer from function in C you must define the local variable as a static variable.

Program 2:
Output:
0x601038
10
Comment
Article Tags: