Pointer and 2d array in C plus plus programming language

Pointer and 2d array in C plus plus programming language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

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

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram