Equivalent Pointer Expression

What is the equivalent pointer expression for referring the same element a[i][j][k][l]

Questions by GauravMore

Showing Answers 1 - 0 of 0 Answers

KS111

  • Jun 21st, 2010
 

Similar to what Shekhar has already mentioned, with a small correction:
To print the value of a[i][j][k][l], we need to use *(*(*(*(a+i) + j) + k) + l).

Explanation:
Let us first take a simple example of a 2 dimensional array.
a[i][j]. Let the values be

a[0][0] = 1
a[0][1] = 2
a[0][2] = 3

a[1][0] = 4
a[1][1] = 5
a[1][2] = 6

a[2][0] = 7

a[2][1] = 8
a[2][2] = 9


Since 'a' is 2 dimensional,
*a would give us '1'.
*(a+1) would give us '4', instead of 2.
The reason is that, a is a pointer to a single dimensional array.
So, it increments to the next single dimensional array.
*a would be pointing to the values set '1  2  3'
*(a+1) points to '4  5  6'
*(a+2) points to '7  8  9'

From here, it is like single dimensional array.
So we have +1 incrementing to the next element.
So, value at *(a+2) + 1, i.e., *( *(a+2) + 1) will give us 8, which is a[2][1]
so *(*(a+i)+j) = a[i][j].

Similar concept can be applied for any dimensional array.
*(*(*(*(a+i) + j) + k) + l) = a[i][j][k][l]

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions