How to return a Pointer from a Function in C Last Updated : 19 Aug, 2020 Comments Improve Suggest changes Like Article Like Report 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: C // C program to illustrate the concept of // returning pointer from a function #include <stdio.h> // Function returning pointer int* fun() { int A = 10; return (&A); } // Driver Code int main() { // Declare a pointer int* p; // Function call p = fun(); printf("%p\n", p); printf("%d\n", *p); return 0; } Output: Below is the output of the above program: 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: C // C program to illustrate the concept of // returning pointer from a function #include <stdio.h> // Function that returns pointer int* fun() { // Declare a static integer static int A = 10; return (&A); } // Driver Code int main() { // Declare a pointer int* p; // Function call p = fun(); // Print Address printf("%p\n", p); // Print value at the above address printf("%d\n", *p); return 0; } Output: 0x601038 10 Comment More infoAdvertise with us Next Article How to return a Pointer from a Function in C S Subhajit Guha Thakurta Follow Improve Article Tags : C Language C-Pointers C-Functions Similar Reads How to Declare a Pointer to a Function? A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w 2 min read How to Pass or Return a Structure To or From a Function in C? A structure is a user-defined data type in C. A structure collects several variables in one place. In a structure, each variable is called a member. The types of data contained in a structure can differ from those in an array (e.g., integer, float, character). Syntax: struct geeksforgeeks { char nam 3 min read How can I return multiple values from a function? In C programming, a function can return only one value directly. However, C also provides several indirect methods in to return multiple values from a function. In this article, we will learn the different ways to return multiple values from a function in C.The most straightforward method to return 3 min read Return From Void Functions in C++ Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but the 2 min read Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di 6 min read Like