tags:

views:

98

answers:

4
+6  A: 

*x refers to the first array ({1,2,3,4,5}), and is equivalent to x[0]. Adding one to x move to the next array, so *(x+1) would refer to the second array, and would be equivalent to x[1].

*(*x + 2) is therefore the third element in the first array, which is 3. This means that *(*x + 2) + 5 is equal to 8.

The parentheses matter a lot, for example *(*(x+2)) would be the first element in the third array.

*(*x + 2) results in the same value as *(*x) + 2, but does not use the same element of the array.

bde
`*x` is not a pointer, it is an array. It might decay to a pointer in some cases, but not always. For example, try `sizeof *x`.
Alok
Good point, I'll change the wording.
bde
+1  A: 

x is a int** so it's like if you have a first layer of pointers and everyone of them point to a int* (so an array of int).

When you write *x you obtain the address that contains the address which points to the first row of your multi dimensional array.

So if you take (*x + 2) if it's like referencing to first row of you array and then add 2 to the address: you obtain the address of the third element of first row. But since this is still a pointer you add an external *(*x+2) to exactly obtain third element of first row.

Jack
`x` is not an `int **`. `x` can never even decay to `int **`.
Alok
+4  A: 
  1. *x is a dereference operation. In other words, "give me what x is pointing at". Since this is an array (of arrays), dereferencing x will give you the first array. This is equivalent to the array access syntax of x[0].

  2. *(*x+2)+5 is equivalent to x[0][2] + 5, which gives you 8. This is because: *x is the same as x[0] (see #1) and *(x + 2) is the same as x[2]. Once you've done two dereferences, you've gone from an array of arrays (similar to a double-pointer) to an array (single pointer) to an actual number (the third item in the first array). Then, it's just 3 + 5 = 8.

  3. *(*x+2) is equivalent to x[0][2] (see #2), which is 3 (third element in array). However, *(*x) + 2 gives you x[0][0] + 2 (first element in array plus 2), which is 1 + 2 = 3. Same answer, but very different way of getting it.

Bob
You mean if I have *(*x+1) I would first find where it points to? i.e x[0][0] and then add 1 to the value which it points to?Wont it be the same as *(*x)+1 ?
fahad
@fahad *(*x+1) would be x[0][1], which is the second element in the first array. *(*x)+1 means to take x[0][0] and add 1 to the value. Again, in your particular case, they are both equal to 2.
Bob
@Bob:thanks alot
fahad
+1  A: 

Think of it this way:

typedef int Int5[5];
Int5 x[3];

x is an array with 3 elements. Each of those three elements is a array of 5 ints.

  • What does *x refer to?

x is the same as '&x[0]so*xis the same asx[0]` which is the first 5-element array.

  • *(*x+2)+5 refer to "8". How does that happen?

*x is x[0], and x+2 is &x[2] so *x+2 is &x[0][2] and *(*x + 2) is x[0][2] which happens to be 3. Add five to that for 8.

  • Is *(*x+2) same as *(*x)+2?

*(*x+2) is x[0][2] as we've seen. *(*x) would be x[0][0], so *(*x)+2 is x[0][0]+2. So both *(*x+2) and *(*x)+2 end up equaling 3, but that is merely a coincidence.

James Curran