This Lecture, will explain about two important topics of C++ i.e. Pointers & Arrays. This will help you explore the low-level capabilities of C++, and will also enable you for direct memory management.
Array is a basic data structures in C++. It is a collection of elements of the same type, stored in contiguous memory locations.
For example, we can declare an integer array like this:
Here, we have declared an array named ‘arr’ of type ‘int’ with a size of ‘5’. This array can store 5 integers, and as each integer is of 4 bytes, so total memory occupied by this array will be 20 bytes (i.e. 5 elements * 4 bytes per element).
The memory layout for the array looks something like this:
Array stores the elements in consecutive memory locations. The name of the array, ‘arr’, points to the start of these memory location.
An array element can be accessed directly by an index position, using the square bracket
 syntax. As, indexing in C++ starts from 0, so we can access the first element of array by passing 0 in the square brackets
, like this,
It will return the first element of array.
Now, we can access the array element using the pointers to. For example, we can declare an integer pointer and point it to the address of first element of array i.e.
int * ptr = &arr;
Then we can use the deference operator with pointer to access the first element like this,
It will return the first element of array, because pointer
ptr is pointing to the memory address of first element of array.
Similarly, to acesss the second element of array, either we can use the square brackets i.e.
Or we can also, also increment the pointer ptr by 1 and access the second value of array by derefrencing it i.e.
*(ptr + 1)
It will give the second element of array.
ptr is pointing to the memory address of first element of array. So, when we add 1 to the integer pointer, it will start pointing to memory address of next integer.
Suppose memory address of first element of array was 20000, so value in pointer ptr was 20000. When we incremented the integer pointer ptr by 1, it will move the pointer ptr to next integer pointer, basically it will add sizeof(int) i.e. 4 in the 20000 i.e. memory address of first element of array. So,
ptr+1 will point to the memory address 20004 i.e. the mempory address of second element in array.
Similarly, to acesss the ith element of array, either we pass i in the square brackets i.e.
Or we can also, also increment the pointer ptr by i and access the second value of array by derefrencing it i.e.
*(ptr + i)
It will give the element at index position i in array.
As indexing in C++ starts from 0. So,
In the start, we initialized an integer pointer by poiting it to the memory address of first element of array, like this,
int * ptr = &arr;
Instead of accessing the first element of array with square brackets and then accessing its address using ampersend operator, we can directly use the array name and cast it to an integer pointer i.e.
int * ptr = arr;
It will have the same effect i.e. pointer ptr will point to the mempoy address of first element of array. We can use the pointer arthmetic to access the ith element in array using the pointer ptr.
In C++, pointers and arrays are often used interchangeably. But there are certain differences, let’s dicsuss them.
Pointers and arrays in C++ are closely linked and used to manipulate memory and build complex data structures. We can use the pointers to access array elements and manipulate them.