Printing a pointers value

前端 未结 5 2091
囚心锁ツ
囚心锁ツ 2021-01-14 21:25
#include 

int main(void)
{
   int x = 99;
   int *pt1;

   pt1 = &x;

   printf(\"Value at p1: %d\\n\", *pt1);
   printf(\"Address of p1 (with %%         


        
5条回答
  •  Happy的楠姐
    2021-01-14 21:46

    • sizeof( int * ) need not be equal to sizeof( int )

    • the library implementation is free to use a different, more appropriate presentation of the pointer value (e.g. hexadecimal notation).

    • %p is simply "the right thing to do". It's what the standard says should be used for pointers, while using %d is misusing the integer specifier.

    Imagine what happens when someone refactors your code, and somewhat over-enthusiastically exchanges all "int" with "long" and all "%d" with "%ld"... oops.

提交回复
热议问题