How does pointer arithmetic work in case of printing addresses for a 2D array ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radha gogia
    New Member
    • Feb 2015
    • 56

    How does pointer arithmetic work in case of printing addresses for a 2D array ?

    For 2D arrays , Please explain below concepts :

    int main()
    {
    int matrix[2][5] = {{1,2,3,4,5},{6 ,7,8,9,10}};
    printf("%p %p %p ", matrix+1, &matrix+1, &matrix[1][0]);
    return 0;
    }

    I am unable to understand how these expressions work , matrix+1 and &matrix[1][0] return same address , how's that going ? matrix gives the base address , now to it if I add 1 , it should be base address + sizeof type to which matrix is pointing which is an int since matrix holds the base address of the first element , so it should print the address of the &matrix[0][1] , which is 2 nd element .

    And I am not getting how matrix+1 and &matrix+1 are different when &matrix and matrix are printing same addresses .

    Please explain it briefly .
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Take a look at the Arrays Revealed insights article. It may answer all your questions.

    Comment

    Working...