C++ Pointers and Functions
Passing Pointers to Functions
In C++, pointers can be passed as arguments to functions, allowing the function to modify the value pointed to by the pointer.
Here is an example of passing a pointer to a function:
#include <iostream> using namespace std; void doubleValue(int* ptr) { *ptr *= 2; } int main() { int num = 5; cout << "num before function call: " << num << endl; // pass the address of num to the function doubleValue(&num); cout << "num after function call: " << num << endl; return 0; }
In this example, the doubleValue
function takes a pointer to an int
as its parameter. Inside the function, the value pointed to by the pointer is multiplied by 2.
To pass the address of num
to the function, we use the address-of operator &
in the function call. This creates a pointer to num
, which is then passed to the function.
When the function modifies the value pointed to by the pointer, it also modifies the value of num
in the calling function. Therefore, the output of the program is:
num before function call: 5 num after function call: 10
how to pass arrays to functions by using pointers.
In C++, arrays can be passed to functions by using pointers. Here's an example:
#include <iostream> using namespace std; // function that takes an array and its size as arguments void printArray(int *arr, int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); // passing array to function printArray(arr, size); return 0; }
In this example, the printArray()
function takes an array and its size as arguments. The array is passed as a pointer (int *arr
), which allows the function to access the elements of the array. In the main()
function, an array arr
is declared and initialized with values. The sizeof()
operator is used to determine the size of the array in bytes, which is divided by the size of the first element of the array to get the number of elements in the array. The printArray()
function is then called with the arr
array and its size as arguments.
Returning a Pointer from a Function
In C++, you can also return a pointer from a function. To do this, you can declare the function with a return type that is a pointer type, such as int*
, char*
, or a custom defined pointer type. Here is an example of a function that returns a pointer:
int* createArray(int size) { int* arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = i; } return arr; }
This function creates an array of integers of the given size using dynamic memory allocation (using the new
operator), initializes its elements with consecutive integers starting from 0, and then returns a pointer to the first element of the array.
You can call this function and use the returned pointer to access the elements of the array, like this:
int* arr = createArray(5); for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } delete[] arr;
This code creates an array of size 5 using the createArray()
function, prints its elements, and then frees the memory using the delete[]
operator.
Note that it is important to manage the memory allocation properly when returning a pointer from a function, to avoid memory leaks or undefined behavior. In this example, we use new
to allocate memory and delete[]
to free it, but in more complex scenarios you may need to use other memory management techniques such as smart pointers.