So i am new to programming and i have this program that i have to write, it has an array of integers and i have to pass it to a function with a pointer and then with a double pointer. After i wrote the code i had this error that i couldn't find a solution to. The error is :
initialization of 'int **' from incompatible pointer type 'int *' [-Wincompatible-pointer-type]
The code that i wrote is this:
int main(){
int i;
int arr[]={3,-15,19,0,-984};
int *p=arr;
funzione(p,sizeof(arr)/sizeof(arr[0])); //The first function
for(i=0;i<(sizeof(arr)/sizeof(arr[0]));i++) //print of the single pointer (p)
printf("%d\n",p[i]);
int **g=p; //the error results to be coming from p in this row
for(i=0;i<(sizeof(arr)/sizeof(arr[0]));i++) //print of the double pointer (g)
printf("%d\n",g[i]);
funzione2(g,sizeof(arr)/sizeof(arr[0])); //The second function
return 0;
}
My question is how can i remove this error.
The variable
phas the typeint *while the initialized variableghas the typeint **.These pointer types are not compatible and there is no implicit conversion from one pointer type to another.
You need to write
Pay attention to that the for loop can look the same way if you will declare the pointer
galso the following wayAlso the expression
sizeof(arr)/sizeof(arr[0])has the unsigned integer typesize_t. So it would be better to declare the variableialso as having the unsigned typesize_tinstead of the signed typeint.Though it is even much better to declare the variable
iin the minimal scope where it is used as for exampleAnd this called function
is not the first function as it is written in the comment.:)