-2

After some guys proved a[5] == 5[a] is true as
the C standard defines the [] operator as a[b] == *(a + b)

I've been thinking about a multidimensional array, for example:

int x[2][2]={
        {'A','B'},
        {'C','D'}};

I could echo the variables using x[0][0], x[0][1], x[1][0], x[1][1]
But how about using pointers?

7
  • 1
    Please, please, don't do that :) Commented Feb 26, 2014 at 16:11
  • The answer follows directly from the definition of [] that you mentioned. Commented Feb 26, 2014 at 16:11
  • I tried to do it, but I can't figure out so I ask... Commented Feb 26, 2014 at 16:13
  • stackoverflow.com/questions/2003745/… Commented Feb 26, 2014 at 16:13
  • I don't how it happened that This question may already have an answer here Commented Feb 26, 2014 at 16:36

2 Answers 2

4

Here are the answers:

x[0][0] ➜ *(x[0]+0) ➜ *(*(x+0)+0)
x[0][1] ➜ *(x[0]+1) ➜ *(*(x+0)+1)
x[1][0] ➜ *(x[1]+0) ➜ *(*(x+1)+0)
x[1][1] ➜ *(x[1]+1) ➜ *(*(x+1)+1)

Explanation: Fetching/Echoing a value from inside an addrress uses the dereference pointer * and should follow the address where the values would come from.

Let's take x[1][0] as an example:

  • We are fetching the address of x[1] + an offset value, or how far we would move from the current address, our offset is 0 and thus the address of x[1][0] using pointer is x[1]+0
    And to fetch the value would would add the * operator to call for the value inside the address and now we have *(x[1]+0)

But we still fetch the first address x[1] using square bracket [] operator, so using pointers...

  • We would fetch x[1][0] using pointers by moving from offset to offset, the only thing we need to do now is to convert x[1] inside *(x[1]+0) so it would be *(*(x+1)+0)

See it working, Click Here
The same process happens on multidimensional arrays

Also... You can simply the equation whenever posssible

Can be simplified : *(*(x+0)+0)*x,
                              *(*(x+1)+0)*(x+1)
                              *(*(x+0)+1), ➜ *(*x+1)
Cannot be simplified : *(*(x+1)+1)

Reasons why? As have mentioned we are moving from one offset to another and we're not adding the offset all together

  • *(*(x+0)+0) Obviously, did not move an offset so it could be simple as *x
  • *(*(x+1)+0) This offset on the inner address have moved *(x+1) but the outer did not +0 so it could be *(x+1) itself
  • *(*(x+0)+1) This offset on the inner address did not moved x+0 but the outer have moved +1 so it could call the *(x+0) to be *x thus making it *(*x+1)
  • *(*(x+1)+1) This one cannot be simplified anymore 'cause we need to move 1 offset on the inner pointer call and move 1 offset again afterwards.
Sign up to request clarification or add additional context in comments.

8 Comments

Your answer works fine for x[0][0] and x[0][1], but it gives an error for x[1][0] and x[1][1]. In where *(*(x+1)+0) supposed to echo C but it echoes B, also for *(*(x+1)+1) that supposed to echo D, it echoes C.
Sir, how then would I do it on x[1][0] and x[1][0]? Can you please update your answer?
printf("%c %c\n",*(*(x+1)+0),x[1][0]); both prints 'C'. What compiler are you using?
It works fine in Borland TurboC++ too. You might be doing something else wrong. Can you post a minimized version of your source code here?
|
0

Since it is a Two Dimensional Array you should use * two time

*(*(x+0)+1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.