Segmentation fault while using malloc with char pointers

前端 未结 5 1175
独厮守ぢ
独厮守ぢ 2021-01-22 16:20

I am new to C and learning structs. I am trying to malloc a char pointer with size 30 but it is giving a segmentation fault(core dump). I searched it on the interne

5条回答
  •  暖寄归人
    2021-01-22 17:09

    You never allocated space for the struct itself, only a pointer to it.

    Try something like:

    str *p = malloc(sizeof(str));
    

提交回复
热议问题