What's the proper use of printf to display pointers padded with 0s

后端 未结 9 2050
暖寄归人
暖寄归人 2021-01-31 14:32

In C, I\'d like to use printf to display pointers, and so that they line up properly, I\'d like to pad them with 0s.

My guess was that the proper way to do this was:

9条回答
  •  醉梦人生
    2021-01-31 15:08

    It's easy to solve if you cast the pointer to a long type. The problem is this won't work on all platforms as it assumes a pointer can fit inside a long, and that a pointer can be displayed in 16 characters (64 bits). This is however true on most platforms.

    so it would become:

    printf("%016lx", (unsigned long) ptr);
    

提交回复
热议问题