Strange behaviour when printing pointers

后端 未结 4 1258
执念已碎
执念已碎 2020-12-21 18:08

I have the following code:

#include 

typedef struct {
  int* arg1;
  int  arg2;
} data;

int main(int argc, char** argv) {
  data d;
  printf         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-21 18:44

    You should initialize d to get deterministic results:

    data d = {0, 42};
    

    or:

    data d;
    d.arg1 = 0;
    d.arg2 = 42;
    

    Without initialization, using the values leads to undefined behavior.

提交回复
热议问题