Printing a pointers value

前端 未结 5 2037
囚心锁ツ
囚心锁ツ 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 22:02

    If on your system pointers happen not to be the same size as ints, you can cause an unlimited amount of screwage. If they happen to be the same size, then you're probably safe. As far as the language standards go, though, passing a pointer and treating it as an integer is allowed to set fire to your computer, send pornographic email to your boss, or cause demons to fly out of your nose. It probably won't do any of those things, but it could conceivably print out misleading values or something.

    Don't do this. Use %p unless you have a desperate need to treat the address as an integer (note: you probably don't); in that case, actually cast it to an integer type so that you aren't abusing the argument-passing mechanism.

提交回复
热议问题