-1

I have faced two different addresses for a pointer variable.I don't know what they mean.Why was there two different addresses for two outputs?

char *name = "John";
printf("is stored at %p\n",name ); //output that is showed "is stored at 0x558b8c21e9c4"
printf("print on the screen %p\n",&name);//output that is showed "print on the screen 0x7ffd8b9be710"
Barmar
  • 741,623
  • 53
  • 500
  • 612
Atakan
  • 1
  • 1

1 Answers1

1

The variable name contains the address of the string literal "John".

So this call

printf("is stored at %p\n",name );

outputs the address of the first character of the string literal.

The expression &name contains the address of the variable name and has the type char ** instead of the type char * So the second call of printf outputs the address of the variable name itself instead of the address of the string literal.

You should cast outputted pointers to the type void *. For example

printf("is stored at %p\n", ( void * )name );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335