Pointers and Arrays in C++

Pointers and Arrays in C++

Pointers and Arrays in C++ are related to each other. The array name is considered as a pointer that is, the name of an array contains the address of an element. Pointers are the variables that hold addresses of another variable. Pointer stores not only the address of a single variable but also store the address of a group of an array.

 

Syntax of Pointer and Array in C++

data_type (*var_name)[size_of_array];

Example of Pointer and Array in C++

int (*ptr)[10];

Program of Pointer and Array in C++

#include <iostream>
using namespace std;
int main()
{
    int *p;
     
    int (*ptr)[5];
    int arr[5];
     
    p = arr;
     
    ptr = &arr;
     
    cout << "p =" << p <<", ptr = "<< ptr<< endl;
    p++;
    ptr++;
    cout << "p =" << p <<", ptr = "<< ptr<< endl;
     
    return 0;
}

Output

p = 0x7fff4f32fd50, ptr = 0x7fff4f32fd50
p = 0x7fff4f32fd54, ptr = 0x7fff4f32fd64

 

Read more, Pointers in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *