What are the rules for casting pointers in C?

前端 未结 5 636
梦毁少年i
梦毁少年i 2020-12-04 04:35

K&R doesn\'t go over it, but they use it. I tried seeing how it\'d work by writing an example program, but it didn\'t go so well:

#include 

        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 05:17

    You have a pointer to a char. So as your system knows, on that memory address there is a char value on sizeof(char) space. When you cast it up to int*, you will work with data of sizeof(int), so you will print your char and some memory-garbage after it as an integer.

提交回复
热议问题