Table of Contents
Pointers are a special type of variable in C++ that can store memory addresses and perform essential operations. They provide full control and flexibility to the programmer to write memory-efficient programs.
Important features like Dynamic Memory Allocation, and passing values by address are only possible through pointers. They also form the foundation for major data structures in computer science.
Representation of variables in memory
All the necessary data required by the program are stored in the main memory. Every byte of data in the memory has a unique address represented as a hexadecimal number.
Therefore, every variable that we create in our program is assigned a memory location where its data is stored. Using pointers, we can access and retrieve the value of a variable by knowing its address in the memory.
Pointers in C++
As mentioned before, a pointer is a special variable that can store the address of another variable. Once we store the address of a variable inside a pointer, we can then retrieve or change the variable’s value using that pointer.
Pointer Declaration and Syntax in C++
The general syntax for declaring a pointer is:
datatype *pointername;
A pointer is declared just like any other variable in C++, but its name is preceded by an asterisk *
symbol. Below are some examples of pointer declaration:
/* declares a pointer to an integer variable */ int * mypointer; /* declares a pointer to a character variable */ char * ptr; /* declares a pointer to a float variable */ float * fptr;
Pointers also have data types like other variables. The pointer’s data type must match the variable’s data type to store its address.
So, an integer type of pointer can only store the address of an integer type of variable.
[adinserter block=”2″]
Working with Pointers
C++ provides two operators(&
and *
) to work with pointers.
“Address of” Operator
The & (address of) operator returns the address/memory location of a variable. The returned address can be stored in a pointer, as shown in the example.
/* declare an int variable and an int pointer */ int myvar, * ptr; myvar = 10; /* store the variable’s address in the pointer variable using the & operator */
Note that an asterisk *
is only required when declaring the pointer. Now, ptr
is pointing to myvar
and can access its value or modify it as explained next.
“At Address” Operator
The * (at address) operator is used with a pointer. A pointer stores the memory address of some variable. To access the value in that stored address, we need to add an asterisk *
before it. It is the same as the variable’s value.
cout<<"Address of the variable(ptr): "<< ptr; /* displays an address like 0x22fe4c */ cout<<"Value of the variable(*ptr): "<< *ptr; /* displays the value of myvar pointed by ptr, which is 10 */
The asterisk *
symbol is used in only two cases with pointers. The first use is for declaring a pointer, and the second use is for accessing the value stored in that address.
/* Pointer *ptr and variable myvar both refer to the same value in memory. So, assigning some other value in *ptr will also reflect in myvar */ * ptr = 20; cout << "myvar: " << myvar; /* now myvar is also 20 */
Similarities between a pointer and variable
If we have a variable and pointer as below:
int num = 10, *p; p = #
Then, *p
and num
are both the same and can be used interchangeably. Similarly, p
and &num
will both return the address of the num
variable.
[adinserter block=”2″]
Arithmetic Operations on Pointers
We can add or subtract integers from pointers and update their memory address. Adding 1 to a pointer makes it a point to the next block of memory, and it depends on the data type of the pointer.
Therefore, if a pointer is of integer type and is currently pointing at the address 5005
, adding 1 will make it 5009
, not 5006 because each integer data type takes 4 bytes of space.
So, adding an integer to a pointer as pointer = pointer + 1
does:
pointer = (pointer's address) + (integer * sizeof(datatype));
It always increments relative to the data type of the pointer. This property is beneficial to use pointers with arrays.
Pointers and Arrays
An array linearly stores values. Elements in the array are stored in adjacent memory locations one after the other.
If an integer array’s first element is stored at location 9000, then 2nd element will be at 9004, then 3rd at 9008, and so on.
Also, the name of an array is a pointer to the first element of the array. So, it stores the address of the first element of the array, which can be assigned to a pointer without using the &
operator.
int arr[5] = { 5, 3, 4, 7, 9 }; int * ptr; /* the name of an array is a pointer to the first element*/ ptr = arr; /* print all the values of the array */ for (int i = 0; i < 5; i++) { /* use asterisk to access the value pointed by ptr */ ; cout << * ptr; /* update pointer’s value to the next element’s address */ ; ptr = ptr + 1; }[adinserter block=”2″]
Passing Pointers to Functions
You can pass pointers around just like other variables. Passing a pointer saves us from creating copies of original variables. If we have a large object or a complex data structure, it is important to use pointers to save memory spaces.
You can access the original value back using the pointer. The example below passes a simple int pointer and updates it inside that function.
#include<iostream> using namespace std; /* declare a pointer as a parameter */ int add(int * p, int num) { /* Updating using pointers modifies the original value */ * p = * p + num; } int main() { int num = 5, * ptr; ptr = & num; /* Pass the pointer to the add function */ add( ptr, 10); cout << "Num is: " << num; /* Ouputs: “Num is 15” */ }
When performing any operation with pointers, we are working with the original copy of the variable. Any data that we modify using a pointer also modify the original variable.
This is useful if a function needs to return multiple values. We can accept pointers and update the values inside the function body.
Points to Remember
- Pointers are a feature in C++ that provides more power, control, and flexibility to write efficient programs.
- A pointer is a special variable that can store the address of another variable.
- A pointer is declared using an asterisk
*
symbol after the data type. - “Address of” operator
&
returns the address of the variable, which can be stored inside a pointer. - Using the “at address” operator
*
, we can access the value stored at the pointer’s address. - You can use Pointers with arrays and also pass them to functions as parameters.
- They are efficient, fast, and have a lot of useful applications in different data structures.