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

Community
  • 1
  • 1
Aesthetic
  • 763
  • 1
  • 14
  • 31
  • 1
    Please, _please_, don't do that :) – Kiril Kirov Feb 26 '14 at 16:11
  • The answer follows directly from the definition of `[]` that you mentioned. – Max Feb 26 '14 at 16:11
  • I tried to do it, but I can't figure out so I ask... – Aesthetic Feb 26 '14 at 16:13
  • http://stackoverflow.com/questions/2003745/pointer-address-in-a-c-multidimensional-array – devnull Feb 26 '14 at 16:13
  • I don't how it happened that **This question may already have an answer here** – Aesthetic Feb 26 '14 at 16:36
  • The answers to that question do a good job explaining what kinds of pointers to multidimensional arrays you can have, and how they are equivalent to indexing. What exactly is the question here? – AShelly Feb 27 '14 at 11:48
  • @AShelly Rikayan Bandyopadhyay's 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` – Aesthetic Feb 27 '14 at 14:02

2 Answers2

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.
Aesthetic
  • 763
  • 1
  • 14
  • 31
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
0

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

*(*(x+0)+1)
prince
  • 1,129
  • 1
  • 15
  • 38