0

I just took an exam for my Computer Science class. On the exam, there was an 2-D int array and we were asked to compute the values. I thought I understood pointer arithmetic, but I got half of them wrong, so I was hoping someone here could explain it better.

I've watched a few Youtube videos and looked at multiple different lecture slides/notes and am still having trouble grasping the concept when it is a 2-D array, I understand a one dimensional one.

int a[2][3] = {{30,40,50} , {60,70,80}};
show the results of the following:
1. a =
2. a[0] =
3. a + 1 =
4. a[0][0] + 1 =
5. *a[0] + 1 =
6. *(a[0] + 1) =
We are to assume that the addresses start at 0x100.

I gave the following answers:
1. a = 0x100
2. a[0] = 0x100.
3. a + 1 = 0x112.
4. a[0][0] + 1 = 0x112.
5. *a[0] + 1 = 11.
6. *(a[0]+1) = 0x104.
........................................................................
I got 3 half credit for 3, 4 wrong, and 6 wrong. If I could change my answer now, I have no idea why 3 is wrong, no idea why 4 is wrong, and I believe 6 would be 40.

Tristin B
  • 117
  • 1
  • 1
  • 9

1 Answers1

1

“3. a+1 = 0x112” is wrong:

  • a is at 0x100. The elements of a are arrays of three int. int is four bytes, so three int is 12 bytes, so the elements of a are 12 bytes. So a+1, which is one element beyond a, is 0x100 + 12 = 0x10c.

“4. a[0][0] + 1 = 0x112” is wrong:

  • a[0][0] is 30. 30+1 is 31.

“6. *(a[0] + 1) = 0x104” is wrong:

  • a[0] is the first element of a, so it is array of three int containing 30, 40, and 50. As an array in a general expression, it is automatically converted to a pointer to its first element, so it points to 30. Adding 1 yields a pointer to the next element, so a[0]+1 is a pointer to to 40. Then dereferencing that with * produces 40.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Yes, I see what I did now. I did not think of them as Hexadecimals, rather Decimals. So, when I said 0x112, I have the concept correct. I wanted to move 12 bytes up the array index, BUT it is Hex so 12 bytes of increase actually yields 0x10c not 0x112. And I see my mistake with the a[0][0] +1, as this is accessing the actual number inside of the array at the index of [0][0], and then adding one to it. And finally, on question 6(just to clarify) a[0]+1 is the address 0x10c, and then it is being de-referenced so it is the value 40? – Tristin B Feb 12 '19 at 21:02
  • @TristinB: `a[0]+1` points to 0x104, not 0x10c. Dereferencing the pointer to 0x104 produces 40. – Eric Postpischil Feb 12 '19 at 21:07
  • @TristinB: In `a[0]+1`, `a[0]` is an array, so it is converted to a pointer to its first element, `&a[0][0]`. So `a[0]+1` is `&a[0][0]+1`. The left side of the `+` is a pointer to an `int`, and the right side is 1, so the sum is a pointer to 1 int further, which is `&a[0][1]`. (Not `a[0][0+1]`, which is the element, but `&a[0][1]`, which is a pointer to the element.) Then `*(a[0]+1)` is `*(&a[0][1])`, which is `a[0][1]`, which is 40. – Eric Postpischil Feb 12 '19 at 21:22