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.

2

3 Answers 3

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]);
Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

-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 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.