-1

I have the following program in C:

void main() {
    int x[3];
    int* p = &x;
    printf("%d %d \n", p, x);
}

those values for p and x seem to be the same, but i dont understand why, since p is the address of pointer x, shouldn't there be something like *p = x instead of p = x ?

Long Claw
  • 107
  • 2
  • 6

3 Answers3

4

The big problem is that p and &x are different types.

The pointer given by &x is a pointer to the whole array x, not to a single element, and its type is int (*)[3], not int *.

If you want to get a pointer to the first element of x then use &x[0], or plain x (as that decays to &x[0]).

And even while the types of &x and &x[0] are different, they both point to the same location (as your (invalid) printouts indicates). This can be seen if you attempt to visualize the array x in memory:

+------+------+------+
| x[0] | x[1] | x[2] |
+------+------+------+
^
|
&x
|
&x[0]

As I already mentioned in a comment, to print pointers you need to use the %p format specifier. Mismatching formatting specifier and argument type leads to undefined behavior.

What's more, the %p format specifier is for void * pointers, so you need to cast the pointers (unless they are already void * of course):

printf("%p %p \n", (void *) p, (void *) x);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

Your code produce some warning at compilation time :

warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=] 
warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=] 

and your compiled code cause an undefined behaviour and the equal result is due to the Undefined behaviour.

Tips: Your code is wrong conceptually, please read more on pointer, array e type in C language.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35
  • 1
    Technically, `int* p = &x;` makes the code ill-formed rather than causing UB (it shouldn't compile). If we ignore that, `printf("%d %d \n", p, x);` does cause UB. – HolyBlackCat Jan 24 '20 at 09:10
  • @HolyBlackCat `int* p = &x;` falls under "A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined.", so potential UB. – chux - Reinstate Monica Jan 24 '20 at 09:18
3

There are several issues in your program leading to undefined behaviour.

First, array x is not initialized, and accessing it later leads to UB.

Second, x when used as pointer is already the address of the first element of array x (equivalent to &x[0]); Hence &x would be a pointer to that pointer. Third, %d requires an integral value, not a pointer to one; so you need to dereference p and x.

The following program should behave as you expect:

int main() {
    int x[3] = { 1,2,3 };
    int* p = x;
    printf("%d %d \n", *p, x[0]);
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58