Pointers and Arrays In C++

Pointers and Arrays In C++

With the use of examples, we will learn in this course how arrays and pointers relate to one another.

Pointers and arrays have many similarities. In C++, an array’s name is regarded as a pointer, meaning it holds the address of each element. The array name is regarded by C++ as the first element’s address.

For instance, if we establish an array called marks that can hold 20 integer values, marks will include the address of the array’s first entry, or markings[0].

As a result, we can state that the array name (marks) is a pointer that contains the address of the array’s first member.

Pointers are used to store the address of the variable, while arrays are used to store the list of elements.

An array of pointers, then, is a representation of an array that contains the addresses of the elements that make up the array.

When we wish to manipulate the data in our array, we need to use this array of pointers. Because the array is extremely bound in nature, the array of pointers makes manipulation simple for us.

This array of the pointer will store the address of each element that is present in the array. The memory addresses of the array’s elements are stored in an array of pointers.

In the part that follows, we’ll go into greater depth about this. We will learn about the C++array of pointers.

Pointers are variables in C++ that store the addresses of other variables. A pointer can store the address of an array’s cells in addition to the address of a single variable.

Syntax

When declaring an array of pointers in C++, we must first establish an array to hold the addresses of the other components, which refer to values for those addresses. We shall examine its grammar in order to better grasp how to use it while programming; see below;

type *name_of_pointer [size_or_array];

If we want to create an array of pointers using the syntax above, we must first define the type of the array pointer;

Next, we define the name of our pointer, but always remember that in C++, a pointer is defined using the astrict symbol ‘*’; and finally

Example:

We define the size of the array, or how many elements it will contain. We shall practice some syntax for a better understanding; see below;

int *myptr[10];

Think of this illustration:

int *ptr;
int arr[5];

// store the address of the first
// element of arr in ptr
ptr = arr;

In this case, arr is an int array and ptr is a pointer variable. The address of the first member of the array is stored in the variable ptr by the code ptr = arr.

You’ll see that we used arr rather than &arr[0]. This is so as they are both the same. As a result, the code above and the code below are identical.

int *ptr;
int arr[5];
ptr = &arr[0];

The remaining array items’ addresses are provided by &arr[1], &arr[2], &arr[3], and &arr[4].

Point to Each Element in the Array(pointers and arrays)

Let’s say we want to use the identical pointer ptr to point to the array’s fourth element.

In this case, ptr + 3 will point to the fourth element if it points to the first element in the previous example. For instance,

int *ptr;
int arr[5];
ptr = arr;

ptr + 1 is equivalent to &arr[1];
ptr + 2 is equivalent to &arr[2];
ptr + 3 is equivalent to &arr[3];
ptr + 4 is equivalent to &arr[4];

The single pointer can also be used to access the items. For instance,

// use dereference operator
*ptr == arr[0];
*(ptr + 1) is equivalent to arr[1];
*(ptr + 2) is equivalent to arr[2];
*(ptr + 3) is equivalent to arr[3];
*(ptr + 4) is equivalent to arr[4];

Consider the case where we initialised ptr = &arr[2];

ptr - 2 is equivalent to &arr[0];
ptr - 1 is equivalent to &arr[1]; 
ptr + 1 is equivalent to &arr[3];
ptr + 2 is equivalent to &arr[4];

Note: The difference in address between ptr and ptr + 1 is 4 bytes. That’s why—ptr is a pointer to an int piece of data. Additionally, with a 64-bit operating system, int has a size of 4 bytes.
The address between ptr and ptr + 1 is 1 byte if pointer ptr is pointing to char type data. It’s because each character takes up one byte.

Example 1: C++ Pointers and Arrays

// C++ Program to display address of each element of an array 

#include <iostream>
using namespace std;

int main()
{
    float arr[3];

    // declare pointer variable
    float *ptr;
    
    cout << "Displaying address using arrays: " << endl;

    // use for loop to print addresses of all array elements
    for (int i = 0; i < 3; ++i)
    {
        cout << "&arr[" << i << "] = " << &arr[i] << endl;
    }

    // ptr = &arr[0]
    ptr = arr;

    cout<<"\nDisplaying address using pointers: "<< endl;

    // use for loop to print addresses of all array elements
    // using pointer notation
    for (int i = 0; i < 3; ++i)
    {
        cout << "ptr + " << i << " = "<< ptr + i << endl;
    }

    return 0;
}

Output

Displaying address using arrays:
&arr[0] = 0x61fef0
&arr[1] = 0x61fef4
&arr[2] = 0x61fef8
Displaying address using pointers:
ptr + 0 = 0x61fef0
ptr + 1 = 0x61fef4
ptr + 2 = 0x61fef8

Without using the pointer variable ptr, we simply wrote the addresses of the array elements in the aforementioned application.

Following that, we used the pointer ptr to point to the address of a[0], ptr + 1 to point to a[1], and so forth.

The majority of the time, array names become pointers. In plain English, array names are changed into pointers. We can use pointers to access array elements because of this.

But keep in mind that pointers and arrays are not the same things.

Array names occasionally do not degrade to pointers. Visit When does array name doesn’t decay into a pointer for more information.

Example 2: Array name used as a pointer(pointers and arrays)

// C++ Program to insert and display data entered by using pointer notation.

#include <iostream>
using namespace std;

int main() {
    float arr[5];
    
   // Insert data using pointer notation
    cout << "Enter 5 numbers: ";
    for (int i = 0; i < 5; ++i) {

        // store input number in arr[i]
        cin >> *(arr + i) ;

    }

    // Display data using pointer notation
    cout << "Displaying data: " << endl;
    for (int i = 0; i < 5; ++i) {

        // display value of arr[i]
        cout << *(arr + i) << endl ;

    }

    return 0;
}

Output

Enter 5 numbers: 2.5
3.5
4.5
5
2
Displaying data:
2.5
3.5
4.5
5
2

Here,

The user-inputted numbers were initially stored in the array arr using the pointer notation.

cin >> *(arr + I) ;

This code is the same as the following code:

cin >> arr[I];

You’ll see that we’ve used the array name arr for the pointer notation rather than declaring a separate pointer variable.

As is already known, the array’s initial element is indicated by the name arr. As a result, we can consider arr to be acting like a pointer.

The values of arr were then shown using pointer notation using a for loop in a similar manner.

cout << *(arr + i) << endl ;

This code corresponds to

cout << arr[i] << endl ;

you may like :

Inheritance In C++

dhttp://c++

This Post Has 2 Comments

Leave a Reply