Pointers and Arrays in C/C++

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.

Introduction to Array

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:

int arr[5];

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:

IMAGE

Array stores the elements in consecutive memory locations. The name of the array, ‘arr’, points to the start of these memory location.



    Accessing Array Elements using Pointers

    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,

    arr[0];
    

    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[0];
    

    Then we can use the deference operator with pointer to access the first element like this,

    *ptr;
    

    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.

    arr[1]
    

    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.

    As pointer 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.

    arr[i]
    

    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,

    • Index position of 1st element of array is : 0
    • Index position of 2nd element of array is : 1
    • Index position of 3rd element of array is : 2
    • Index position of 4th element of array is : 3
    • ….
    • ….
    • Index position of ith element of array is : i-1

    Important point:

    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[0];
    

    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.

    Pointers and Arrays

    In C++, pointers and arrays are often used interchangeably. But there are certain differences, let’s dicsuss them.

    • When we cast an array to a pointer, then it decays some information. The size of an array is part of its type, and it is also known at compile-time. Whereas, if we cast an array to a pointer, then pointer does not holds the size information of array, and that is called decay of information.
    • When an array id declared, its size is fixed at compile-time; we can’t resize it at run-time. Pointers, however, can be made to point to different memory locations at run-time.
    • Arithmetic on pointers on array will have different behaviour thatn on a pointer.

    Summary

    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.

    Scroll to Top