print bits of a void pointer

后端 未结 6 2156
[愿得一人]
[愿得一人] 2021-01-23 01:52

If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated?

For example:



        
6条回答
  •  無奈伤痛
    2021-01-23 02:35

    The malloc allocates bytes.

     for (i = 0; i < 24; ++i) printf("%02xh ", p[i]);
    

    would print the 24 bytes. But if cast to an int, you'll need to adjust the loop:

     for (i = 0; i < 24; i += sizeof(int)) printf("%xh", *(int *)&p[i]);
    

    But then, if you know you want an int, why not just declare it as an int?

提交回复
热议问题