How to return a Pointer from a Function in C (original) (raw)
Last Updated : 19 Aug, 2020
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;
}
`
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
- How to declare a Two Dimensional Array of pointers in C? A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo 3 min read
- Passing Pointers to Functions in C Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po 2 min read
- Address of a function in C or C++ We all know that code of every function resides in memory and so every function has an address like all others variables in the program. We can get the address of a function by just writing the function's name without parentheses. Please refer function pointer in C for details. Address of function m 1 min read
- Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe 3 min read
- Implicit Return Type int in C In C, every function has a return type that indicates the type of value it will return, and it is defined at the time of function declaration or definition. But in C language, it is possible to define functions without mentioning the return type and by default, int is implicitly assumed that the ret 2 min read