0

Consider this code:

int ar[3] = {1, 2, 3};
int *ar2 = ar;

This code compiles. I know that ar decays into a pointer to an int and it makes sense that I can initialize ar2 with ar.

I want to similarly initialize a two dimensional array but this code doesn't compile:

int ar3[3][2] = {{1, 2}, {3, 4}, {5, 6}};
int **ar4 = ar3;

Why?

Alex
  • 1
  • You may do `int (*ar4)[2] = ar3;`. – Jarod42 Apr 07 '17 at 14:04
  • 1
    `ar3` has a contiguous `int`. With `int**`, you have a contiguous `int*`, but no contiguous `int`. – Jarod42 Apr 07 '17 at 14:06
  • I don't see why this is important. For instance I can have `int **x = &y;` if y is a pointer to an int and there is contiguous `int*` and this compiles. – Alex Apr 07 '17 at 14:15
  • I understand `int (*ar4)[2] = ar3;` but it seems it's necessary to specify the size (`2`). Why? In my first example with one dimensional array I didn't specify size at all. – Alex Apr 07 '17 at 14:19
  • An array is not a pointer. An array of arrays is not a pointer to pointer. – Pete Becker Apr 07 '17 at 14:20

0 Answers0