[Solved] Difference between & and * [duplicate]


&p gets the pointer to the integer p -> That is, the memory address in which p is stored.

*p “dereferences” the pointer, i.e. looks at the object at the memory address provided by p and returns that object.

Your code above is invalid C, as you cannot dereference an int:

error: indirection requires pointer operand ('int' invalid)

Consider instead the following:

// Create an integer
int p = 1234;
printf("Integer: %d\n", p);

// Get the pointer to that integer, i.e. the memory address in which p is stored
int *pointer = &p;
printf("Pointer: %p\n", pointer);

// Dereference the pointer to get the value back
int q = *pointer;
printf("Dereferenced: %d\n", q);

Gives the following output:

Integer: 1234
Pointer: 0x7ffee53fd708
Dereferenced: 1234

Also notice that to print out a pointer address, we have to use a special format specifier %p instead of the %d we’d use for an int.

2

solved Difference between & and * [duplicate]