Pointer and 2d array in C plus plus programming language

Author: Al-mamun Sarkar Date: 2020-04-03 17:22:39

Pointer and 2d array in C plus plus programming language . The following code shows the use of pointer for 2d array in C plus plus Programming Language.

 

Code:

#include<iostream>
using namespace std;

int main()
{
    int A[2][3] = {{1, 2, 3}, {4, 5, 6}};

    int (*p)[3] = A;
    cout << "1 => A is: " << A << " A[0] is : " << A[0] << " *A[0] is : " << *A[0] << endl;

    cout << "2 => *A is: " << *A << " A[0] is : " << A[0] << " &A[0][0] is : " << &A[0][0] << " A[0][0] is : " << A[0][0] << endl;

    cout << "3 => *(A+1) is: " << *(A+1) << " &A[1] is : " << A[1] << " &A[1][0] is : " << &A[1][0] << " A[1][0] is : " << A[1][0] << endl;

    cout << "4 => *(A+1)+2 is : " << *(A+1)+2 << endl;

    cout << "5 => *(*(A+1)+2) is : " << *(*(A+1)+2) << endl;

    // A[i][j] == *( A[i] + j ) == *(*(A + i) + j) Are same
    return 0;
}

 

Output:

1 => A is: 0x7ffeec603a50 A[0] is : 0x7ffeec603a50 *A[0] is : 1
2 => *A is: 0x7ffeec603a50 A[0] is : 0x7ffeec603a50 &A[0][0] is : 0x7ffeec603a50 A[0][0] is : 1
3 => *(A+1) is: 0x7ffeec603a5c &A[1] is : 0x7ffeec603a5c &A[1][0] is : 0x7ffeec603a5c A[1][0] is : 4
4 => *(A+1)+2 is : 0x7ffeec603a64
5 => *(*(A+1)+2) is : 6