AlgoMaster Logo

Pointers and Functions

Last Updated: January 3, 2026

5 min read

Pointers in C++ are a powerful feature, especially when it comes to passing data to functions. You might already be familiar with how to use arrays and pointers, but combining these concepts with functions opens up a whole new world of possibilities.

Let’s dive into how pointers interact with functions in C++, explore their nuances, and understand their practical applications.

Passing Pointers to Functions

One of the most common uses of pointers is to pass them to functions. This allows functions to modify the original data rather than working with a copy. When you pass a pointer, you're essentially passing the memory address of a variable, which can lead to more efficient memory usage and performance.

Basic Example

Consider a simple function that increments an integer. If we pass the integer by value, the function works on a copy, and changes are not reflected outside the function. However, if we pass a pointer, we can modify the original integer.

In this example, the increment function takes a pointer to an integer. By dereferencing the pointer with (*ptr), we can modify the value at that memory address.

Returning Pointers from Functions

In addition to passing pointers to functions, you can also return pointers from functions. This becomes particularly useful when working with dynamically allocated memory, such as arrays.

Dynamic Memory Allocation Example

Let’s create a function that allocates an array dynamically and returns a pointer to it.

Here, createArray allocates an array using the new operator and returns a pointer to the first element. Always remember to free the memory using delete[] to avoid memory leaks.

Pointer to Pointer

Sometimes, you might need a pointer to a pointer, especially in functions that need to modify the address of a pointer passed to them. This is common when handling dynamic arrays or when you want multiple levels of indirection.

Using Pointer to Pointer

Here’s an example of a function that modifies what a pointer points to:

In this case, allocateMemory accepts a pointer to a pointer (int** ptr). By dereferencing the pointer twice, we can both allocate memory and set the value.

Arrays and Pointers in Functions

When working with functions, it's crucial to understand how arrays interact with pointers. In C++, arrays decay to pointers when passed to functions. This means that the function receives a pointer to the first element of the array.

Example with Arrays

Let’s create a function that takes an array of integers and modifies its elements:

In doubleValues, we accept an array as a parameter. Internally, this is treated as a pointer to the first element, allowing us to modify the original array's values.

Best Practices and Common Pitfalls

While pointers offer powerful capabilities, they come with their own set of challenges. Here are some best practices and common pitfalls to keep in mind:

Best Practices

  • Initialize Pointers: Always initialize pointers. Uninitialized pointers lead to undefined behavior.
  • Check for Nullity: Before dereferencing pointers, check if they are null to avoid segmentation faults.
  • Avoid Memory Leaks: Always pair new with delete and new[] with delete[].

Common Pitfalls

As a rule of thumb, if you find yourself feeling confused about the state of your pointers or the ownership of memory, take a step back and clarify your logic. Understanding the flow of memory and ownership can prevent many headaches.