0
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)?

2 Answers 2

2

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].

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.