in , ,

C++ Pointers and Arrays

C++ Pointers and Arrays
C++ Pointers and Arrays

C++ pointers arrays: In this tutorial, we will find out about the relation among arrays and pointers with the help of examples.

In C++, pointers are variables that hold addresses of different factors. Not exclusively can a pointer store the location of a single variable, it can likewise store the address of cells of an array.

Think about this example:

int *ptr;
int arr[5];

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

Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of the first element of the array in variable ptr.

Notice that we have used arr rather than &arr[0]. This is on the grounds that both are the equivalent. In this way, the code beneath is equivalent to the code above.

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

The addresses for the rest of the array elements are given by &arr[1], &arr[2], &arr[3], and &arr[4].


Highlight Every Array Elements

Assume we have to highlight the fourth component of the array using a similar pointer ptr.

Here, if ptr points to the principal component in the above model, at that point ptr + 3 will highlight the fourth component. 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];

Thus, we can get to the elements using the single pointer. 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];

Suppose if we have initialized ptr = &arr[2]; then

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 address among ptr and ptr + 1 contrasts by 4 bytes. It is on the grounds that ptr is a pointer to int data. What’s more, the size of int is 4 bytes in a 64-bit operating system.

Similarly, if pointer ptr is pointing to char type data, then the address between ptr and ptr + 1 is 1 byte. It is because the size of a character is 1 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

In the above program, we first just printed the addresses of the array elements without using the pointer variable ptr.

Then, we used the pointer ptr to point to the address of a[0], ptr + 1 to point to the address of a[1], and so on.

In many settings, array names rot to pointers. In straightforward words, array names are changed over to pointers. That is the motivation behind why we can use pointers to get to elements of arrays.

However, we should remember that pointers and arrays are not the same.

There are a few cases where array names don’t decay to pointers. To learn more, visit: When does the array name doesn’t decay into a pointer?


Example 2: Array name used as a pointer

// 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,

1. We initially used the pointer documentation to store the numbers entered by the user into the array arr.

cin >> *(arr + i) ;

This code is equivalent to the code below:

cin >> arr[i];

Notice that we haven’t pronounced a different pointer variable, but instead, we are using the array name arr for the pointer notation.

As we definitely know, the cluster name arr focuses on the primary component of the array. Thus, we can consider arr acting like a pointer.

2. Similarly, we then used for loop to display the values of arr using pointer notation.

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

This code is equivalent to

cout << arr[i] << endl ;

Thanks for watching! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How To Make A Letterhead in CorelDRAW

How To Make A Letterhead in CorelDRAW

How to Make Business Cards

How To Make Business Cards in CorelDRAW