Reputation: 232
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%u ",(*(a+i)+j));
}
cout<<endl;
}
Assuming that 2-d array a is declared, how (*(a+i)+j)
gives the address of each element? I am not understanding this concept. Usually *(a+i)
should give the value at location (a+i)
?
Upvotes: 0
Views: 164
Reputation: 3248
Here it is the case of Row Major ordering where the 2-D array is stored row wise.
Assuming the indexing starts from 0, to access any element in the array, say A[i][j], we have to first cross "i" number of rows and then "j" number of columns.
Since 'a' is the base address of the array, *( a + i ) gives the location in the memory after i rows ,i.e ith row and adding j to it takes the pointer to the jth column of the ith row giving the address of A[i][j].
Upvotes: 2
Reputation: 8292
yes *(a+i) gives the value at that location which is pointer to the beginning of the ith row and + j takes the pointer from the beginning of the row to the jth element of the ith row.
So i is taking care of the rows and j is taking care of the columns.
Hence (*(a+i) + j) is giving the address of each element. Hope this clears.
Upvotes: 1