Dereferencing pointers without pointing them at a variable

前端 未结 4 1299
野趣味
野趣味 2020-12-02 01:14

I\'m having trouble understanding how some pointers work. I always thought that when you created a pointer variable (p), you couldn\'t deference and assign (*p = value) unle

4条回答
  •  被撕碎了的回忆
    2020-12-02 01:50

    I always thought that when you created a pointer variable (p), you couldn't deference and assign (*p = value) unless you either malloc'd space for it (p = malloc(x)), or set it to the address of another variable (*p = &a)

    For practical purposes you thought right. Better keep thinkig that.

    If you do what in in your example the behavior of the program is undefined. What means anything can happen, including a misleading "works" feeling. But it is nothing but recipe for disaster.

提交回复
热议问题