C++ Function Returning Pointer
In C++, a function can return a pointer. This is useful when you want to return the address of a variable or an array, allowing the caller to access or manipulate the data directly.
Functions that return pointers must handle memory carefully to avoid undefined behavior.
Syntax
The syntax to define a function that returns a pointer is given below.
</>
Copy
return_type* function_name(parameter_list) {
// Function body
return pointer_variable;
}
- return_type*
- Specifies that the function returns a pointer to the specified type.
- function_name
- The name of the function.
- parameter_list
- Optional, specifies the input parameters to the function.
- return pointer_variable
- The function returns a pointer to a variable or array.
Examples
Example 1: Returning a Pointer to a Static Variable
This example demonstrates how to return a pointer to a static variable from a function.
</>
Copy
#include <iostream>
using namespace std;
int* getStaticValue() {
static int value = 42; // Static variable
return &value; // Return pointer to the static variable
}
int main() {
int* ptr = getStaticValue();
cout << "The value is: " << *ptr << endl;
return 0;
}
Output
The value is: 42
Explanation
static int value = 42;
: A static variable is declared, ensuring it persists in memory for the duration of the program.return &value;
: Returns the address of the static variable to the caller.int* ptr = getStaticValue();
: The pointerptr
stores the address returned by the function.*ptr
: Dereferences the pointer to access the value stored at the address, which is42
.
Example 2: Returning a Pointer to a Dynamically Allocated Variable
This example demonstrates how to return a pointer to a dynamically allocated variable using new
.
</>
Copy
#include <iostream>
using namespace std;
int* createDynamicValue() {
int* ptr = new int(99); // Dynamically allocate memory
return ptr; // Return the pointer to the allocated memory
}
int main() {
int* ptr = createDynamicValue();
cout << "The value is: " << *ptr << endl;
delete ptr; // Free the allocated memory
return 0;
}
Output
The value is: 99
Explanation
int* ptr = new int(99);
: Dynamically allocates memory for an integer and initializes it with the value99
.return ptr;
: Returns the address of the dynamically allocated memory to the caller.int* ptr = createDynamicValue();
: Stores the returned pointer inptr
.*ptr
: Dereferences the pointer to access the value stored in the dynamically allocated memory.delete ptr;
: Frees the allocated memory to prevent memory leaks.
Points to Remember about Functions that Return a Pointer
- Returning pointers allows functions to return the address of variables or memory locations.
- For static variables, ensure that their lifetime is appropriate for the use case.
- For dynamically allocated memory, always ensure proper deallocation using
delete
ordelete[]
to avoid memory leaks. - Do not return pointers to local (non-static) variables, as they are destroyed when the function scope ends, leading to undefined behavior.