0

I'm learning about pointers in C and from what I understood *pointer references to the value of the pointer and pointer references to the memory adress of the pointer. So what made me confused is the following situation

int x = 10;
int *pointer = &x;
printf("pointer = %i ---- *pointer = %i", pointer, *pointer);

The output we get is "pointer = 2293572 ---- *pointer = 10" if you passed the memory adress of the x variable to "*pointer" (the value of pointer) why doesnt it print *pointer = 2293572?

1 Answers1

4

%p is meant for (void*) pointers whereas %i is for (signed) ints. Using %i for a pointer can create issues with printing when sizeof(int) != sizeof(&int) - which is frequently the case on 64-bit machines.

Instead of this:

printf("pointer = %i ---- *pointer = %i", pointer, *pointer);

This:

printf("pointer = %p ---- *pointer = %i", (void*) pointer, *pointer);
Ted Lyngmo
  • 60,763
  • 5
  • 37
  • 77
selbie
  • 91,215
  • 14
  • 97
  • 163