Pointers in C++: An Introduction with Code Samples

CodeCommander
3 min readFeb 20, 2023

--

One of the most important C++ features that enable us to access and manipulate memory is pointers. The memory addresses of other variables or data structures are stored in pointers. We will delve into pointers in C++ and discover how to use them.

What are pointers?

A pointer is a variable that holds track of another variable’s memory address. In C++, the “*” symbol is used to declare a pointer. For instance, the following would be done to declare a pointer to an integer:

int* ptr;

It declares an integer-pointing pointer with the name “ptr.” .however, since “ptr” hasn’t been given a value yet, it currently points to nothing.
A Pointer’s Value is Assigned
We must utilise the “&” operator, which returns the address of a variable, to give a value to a pointer. Let’s imagine, for example, that we want “ptr” to point to the integer variable “num” and that we have one. We’d do out:

int num = 42;
int* ptr = #

Right now, “ptr” is pointing to “num’s” memory address. The “*” operator allows us to retrieve the value of “num” through “ptr.” For instance:

cout << *ptr << endl; // Outputs 42

This generates a value for “num” of 42.

In C++, there are various pointer types, including:

1. Nul Pointers

There are situations when we want to declare a pointer without immediately assigning it to a variable. This can be accomplished by setting the pointer’s initial value to “nullptr,” which is a null pointer. For example:

int* ptr = nullptr;

This declares a pointer with the name “ptr” but no current value.

2. Empty/void Pointers:

Any type of data can be pointed to by a void pointer. When the type of data the pointer will be pointing to is not known at the time of the pointer’s declaration, it is frequently utilised.


void *ptr;

Here, the void pointer ptr has been declared. Any type of data address can be stored in it.

3. Pointer to pointer:

A pointer that points to another pointer is known as a pointer to a pointer. It is frequently employed in intricate data structures like linked lists and trees. Here is an explanation of how to use a pointer to a pointer:

#include <iostream>

using namespace std;

int main() {
int num = 10;
int *ptr = &num;
int **dptr = &ptr;

cout << "Value of num = " << num << endl;
cout << "Value of ptr = " << *ptr << endl;
cout << "Value of dptr = " << **dptr << endl;
cout << "Address of num = " << &num << endl;
cout << "Address of ptr = " << ptr << endl;
cout << "Address of dptr = " << dptr << endl;

return 0;
}

In the previous example, the variable num has the value 10. The address of num is used to set the pointer variable ptr after its declaration. The address of ptr is used to create a double pointer, which we declare as dptr. The address of num is now where the double-pointer dptr refers.

Next, after dereferencing the pointers using * and **, we print the values of num, ptr, and dptr. Using the address-of operator & and the pointers themselves, we can additionally output the addresses of num, ptr, and dptr.

Output:

Value of num = 10
Value of ptr = 10
Value of dptr = 10
Address of num = 0x7ffee3e6c1dc
Address of ptr = 0x7ffee3e6c1dc
Address of dptr = 0x7ffee3e6c1d0

In C++, the pointer-to-pointer concept is quite useful. It enables the efficient creation of dynamic data structures and the execution of operations on them. Yet it’s crucial to use this idea carefully and stay away from mistakes like memory leaks and split problems.

4. Pointers to Functions

A pointer that refers to a function is known as a function pointer. A function can be passed as a parameter to another function very frequently.

Using Pointers to Call by Value

A copy of the variable is created when a variable is passed as an argument to a function, and any changes made to the copy do not impact the original variable. This method is called “call by value.” To change the original variable, however, we can use pointers to pass values by reference. Here’s an example:

Sign up to discover human stories that deepen your understanding of the world.

--

--

CodeCommander
CodeCommander

Written by CodeCommander

Learn programming techniques for C++, JavaScript, CSS, HTML and more through my informative articles .

No responses yet

Write a response