Using a simple sample that involves an array point generates the following warnings. The code codes compile and execute which also leads to questions.
This is the int Array. int numbers[3] = { 101, 202, 303};
These 2 calls generate the warnings.
int *p2 = &numbers;
int **p3 = &numbers;
The following are the Warning generated.
> cc -o myApp test.c
test.c: In function 'main':
test.c:9:12: warning: initialization from incompatible pointer type [enabled by default]
int *p2 = &numbers;
^
test.c:10:13: warning: initialization from incompatible pointer type [enabled by default]
int **p3 = &numbers;
^
The code used is the following :
#include <stdio.h>
int main ()
{
int numbers[3] = { 101, 202, 303};
int size = sizeof(numbers) / sizeof (numbers[0]);
int *p = numbers;
int *p2 = &numbers;
int **p3 = &numbers;
int *p4 = &numbers[0];
int *end = p + size;
printf("p = 0x%d\n", p);
printf("p2 = 0x%d\n", p2);
printf("p3 = 0x%d\n", p3);
printf("numbers = 0x%d\n", numbers);
printf("&numbers = 0x%d\n", &numbers);
printf("numbers[0] = 0x%d\n", numbers[0]);
printf("&numbers[0] = 0x%d\n", &numbers[0]);
for (; p != end; p++)
{
printf ("0x%d\n", p);
}
}
When executed, here is the output.
> ./runTest
p = 0x-207214704
p2 = 0x-207214704
p3 = 0x-207214704
numbers = 0x-207214704
&numbers = 0x-207214704
numbers[0] = 0x101
&numbers[0] = 0x-207214704
0x-207214704
0x-207214700
0x-207214696
Both p2 and p3 as well as p all point to the same address. I really thought using p3 as a double pointer would have resolved the Warning, but it appears not. Why does p2 and p3 generate the Warnings and what is it trying to say?
&numbersis of typeint (*)[3], which is not the same asint **.%pformat specifier and cast the pointer to typevoid *(or a qualified version ofvoid *), e.g.printf("p = %p\n", (void *)p);. (Your implementation of C might also allow you to cast function pointers tovoid *and might also work with the%pformat specifier without casting at all, but all that is non-standard.)