Segmentation Fault in strcpy()

后端 未结 3 791
耶瑟儿~
耶瑟儿~ 2020-11-30 14:29

I have a basic structure like this

typedef struct struck {
    char* id;
    char* mat;
    int value;
    char* place;
} *Truck;

And afunc

3条回答
  •  时光说笑
    2020-11-30 15:28

    Your usage of sizeof is incorrect. In general, the argument to malloc() needs to be "the size of that which the returned pointer is pointing at". In other words, you need sizeof *nT. See how that also eliminates repeating the type name (Truck)?

    Also, in C you don't need to cast the return value of malloc(); it serves no purpose, can hide an actual error, and makes the code harder to read.

    As others have pointed out, you're also not allocating space for any string data, all you have are the pointers in your structure.

提交回复
热议问题