0
#include<stdio.h>
void main()
{
    int a[] = { 1, 2, 3, 4 };
    int b[] = { 5, 6, 7 };
    int *p[2];
    p[0] = a;
    p[1] = &b + 1;
    printf("%d\n%d", &p[1][0], p[0][1]);
}

Here p is a 1d array of pointers, then how come a 2d array is used in the printf statement. Also the output is 1 2.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174

2 Answers2

2

Here p is a 1darray of pointers

Yep, and you can use the subscript operator ptr[index] with pointers which is equivalent to *(ptr + index)

Thus p[1][0] is the same as *(p[1] + 0) which is the same as *(p[1])

Also your code does not compile for several reasons including void main()


Simple example to illustrate:

int main()
{
  const char *hello = "hello";
  const char *world = "world";

  const char *array[2] = {hello, world};

  char e = hello[1]; // char e now contains 'e'
  e = array[0][1]; // same thing as previous line

  char r = world[2]; // char r now contains 'r'
  r = array[1][2]; // same thing as previous line
}
Drax
  • 12,682
  • 7
  • 45
  • 85
  • but p[1][1] is *(p[1] + 1) –  Oct 08 '14 at 13:45
  • then also output is 2 –  Oct 08 '14 at 13:46
  • 1
    @SusanSweedyk First please don't spam, second i don't understand what you are asking ? – Drax Oct 08 '14 at 13:53
  • i m asking since p is declared 1 d array how can we use it as 2 d array –  Oct 08 '14 at 13:57
  • @SusanSweedyk As stated in my answer, because it is an array of pointer, and a pointer can be used with the subscript operator `[]` the same way we use an array. In short you can use an array of pointer the same way you would use an array of array (this is not 100% true though, it's just to make it simple). – Drax Oct 08 '14 at 13:59
  • Looks like no one noticed it, but OP have UB in his code: `p[1] = &b + 1;` (For me `p[1] = &b + 1;` evaluates as random garbage number.) Here `p[1]` contains address of nonexistent `b[3]`. Why? Because `&b` is the address of whole `b` array. When you increment it, you get the address of `b[3]`. OP should use `p[1] = b + 1;`. @Drax , I suggest you to add this info to your answer. – HolyBlackCat Oct 08 '14 at 14:05
  • @SusanSweedyk i added a simple example to illustrate – Drax Oct 08 '14 at 14:10
  • @HolyBlackCat As stated in my answer the code doesn't compile for several reasons and one of the reason is `p[1] = &b + 1;` on my plateforme (clang 3.5) – Drax Oct 08 '14 at 14:11
0

The subscript operator [] is defined for pointer expressions as well as array expressions (which are implicitly converted to pointer expressions before the subscript is applied).

The expression a[i] is evaluated as *(a + i); a is a pointer, and a + i gives the address of the i'th element of the array (pointer arithmetic is based on the pointed-to type; if a points to an int, then a + i gives the address of the i'th int following the one pointed to by a).

So given your array of pointers:

int *p[2];

the expression p[0] has a pointer type, so you can add an offset and dereference the result: *(p[0] + 1), which is the same as writing p[0][1].

John Bode
  • 119,563
  • 19
  • 122
  • 198