0

I wrote the following code declaring an array of 5 pointers to int and then a pointer to the address of the mentioned array:

int* arr[5];
int** p=&arr;

At compilation with gcc, I get the following warning:

initialization from incompatible pointer type [-Wincompatible-pointer-types] int** p=&arr;

Why is int** p the wrong pointer type, isn't &arr a pointer to a pointer to an int?

Many thanks.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
korppu73
  • 237
  • 1
  • 2
  • 5

3 Answers3

0

change int **p = &arr to either int **p = &arr[0] or int **p = arr

In the second option array name arr is converted to a pointer to its first element arr[0] (i.e. a pointer to a pointer to an int).

Soumen
  • 1,068
  • 1
  • 12
  • 18
0

In the statement

int* arr[5];

arr is array of 5 int pointer. Let's say it's pointing to a which is array of 5 integer. It looks like

int a[5]=  {1,2,3,4,5};
int* arr[5] = {a,a+1,a+2,a+3,a+4};

Now when you write

int** p=&arr;

which is wrong as p is of int** type and it should point to arr not to &arr.

isn't &arr a pointer to a pointer to an int? No, it's not, &arr is pointer to an array of pointers to int a[0] etc.

Correct way is

int **p= arr;

Now you can print the array a elements using p as

printf("%d %d\n",*p[0],*p[1]);
Achal
  • 11,821
  • 2
  • 15
  • 37
-4

int* arr[5] is a pointer to a pointer to an int, so &arr is a pointer to a pointer to a pointer to an int.

  • 1
    `int* arr[5]` is not a pointer to a pointer. It is an array of pointers. So `&arr` is a pointer to an array of pointers to int. – interjay Jun 14 '18 at 09:02