Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Pointer and Arrays

In this tutorial, we will learn about the relation between arrays and pointers and how we use them with the help of examples.

Pointer and Arrays

  • In C++, Pointers are variables that hold addresses of other variables. Not only can a pointer store the address of a single variable, it can also store the address of cells of an array.
  • Array and Pointers are vary closely related to each other.
  • The name of an array is considered as a pointer, i.e, the name of an array contains the address of an element.
  • C++ considers the array name as the address of the first element.

Consider this example:

int *p;
int arr[5];

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

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

Notice that: we have used arr instead of &arr[0]. This is because both are the same. So, the code below is the same as the code above.

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

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


Pointing to Every Element of an Array

Suppose we need to point to the fourth element of the array using the same pointer p.

Here, if p points to the first element in the above example then p + 2 will point to the third element. For example,

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

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

Similarly, we can access the elements using the single pointer. For example,

// use dereference operator
*p == arr[0];
*(p + 1) is equivalent to arr[1];
*(p + 2) is equivalent to arr[2];
*(p + 3) is equivalent to arr[3];
*(p + 4) is equivalent to arr[4];
Working of C++ Pointers with Arrays
Working of C++ Pointers with Arrays

Note: The address between p and p + 1 differs by 4 bytes. It is because p is a pointer to an int data. And, the size of int is 4 bytes in a 64-bit operating system.

Similarly, if pointer p is pointing to char type data, then the address between p and p + 1 is 1 byte. It is because the size of a character is 1 byte.


Example 1: C++ Traversing the Array using Ponters

#include <iostream>
using namespace std;
int main() {
    int *ptr;  // Integer pointer declaration
    int arr[] = {1, 1, 2, 3, 4};

    cout << "The Traversing of array: ";

    ptr = arr;
    for (int i = 0; i < 4; i++) {
        cout << *ptr <<endl;
        // ++ moves the pointer to next int position
        ptr++;
    }

    return 0;
}

Output

The traversing of array is 
1
2
3
4
5

Example 2: Program to display address of each element of an array

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;
int main(){
    float arr[2];
    // declare pointer variable
    float *p;

    cout << "Displaying address using arrays: " << endl;

    // use for loop to print addresses of all array elements
    for (int i = 0; i < 2; ++i){
        cout << "&arr[" << i << "] = " << &arr[i] << endl;
    }
    
    p = arr; // it can also be written as: p = &arr[0]

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

    // use for loop to print addresses of all array elements using pointer notation

    for (int i = 0; i < 2; ++i){
        cout << "p + " << i << " = "<< p + i << endl;
    }

    return 0;
}

Output

Displaying address using arrays: 
&arr[0] = 0x7ffe73135d70
&arr[1] = 0x7ffe73135d74

Displaying address using pointers: 
p + 0 = 0x7ffe73135d70
p + 1 = 0x7ffe73135d74

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

Then, we used the pointer p to point to the address of arr[0]p + 1 to point to the address of arr[1].

In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That’s the reason why we can use pointers to access 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 array name doesn’t decay into a pointer?


Example 3: Program to insert and display data entered by using pointer notation.

#include <iostream>
using namespace std;
int main() {
    float arr[4];

   // Insert data using pointer notation
    cout << "Enter the 4 numbers: " << endl;
    for (int i = 0; i < 4; ++i) {
        cin >> *(arr + i); // store input number in arr[i]
    }

    // Display data using pointer notation
    cout << "Displaying the data: " << endl;
    for (int i = 0; i < 4; ++i) {
        // display value of arr[i]
        cout << *(arr + i) << endl ;
    }

    return 0;
}

Output

Enter the 4 numbers: 1
2.5
3.0
4
Displaying the data: 
1
2.5
3.0
4

Here,

  1. We first used the pointer notation 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 declared a separate pointer variable, but rather we are using the array name arr for the pointer notation.

    As we already know, the array name arr points to the first element of the array. So, we can think of arr as 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 ;
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!