mat[i][j] = *(*(mat + i) + j) here mat is a pointer to an array of r elements an

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhi17
    New Member
    • Feb 2010
    • 6

    mat[i][j] = *(*(mat + i) + j) here mat is a pointer to an array of r elements an

    int mat[r][c];
    mat[i][j] = *(*(mat+i) + j) ???

    here mat is a pointer to an array of r elements and each elementof mat itself is an array of c elements
    then in mat +i ,i gets scaled by c*sizeof(int) thus mat+i is starting address of ith row and then dereferencing it yields first element of ith row but looking at this expression it seems that *(mat + i) is also treated as a address
    please help on understanding this way of accessing array elements of multidimensiona l array
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    int mat[r][c];
    mat[i][j] = *(*(mat+i) + j) ???
    *(*(mat+i) + j) is the pointer syntax for mat[i][j]. That is, take the address of mat and add i * c*sizeof(int) to get the address of mat[i]. Then add j * sizeof(int) to get the adress of the element itself, then de-reference that address to get the actual element.

    You might read the article Arrays Revealed in the C Insights Forum.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      That's Arrays Revealed.

      Comment

      Working...