Allocate memory for a struct with a character pointer in C

后端 未结 6 436
礼貌的吻别
礼貌的吻别 2021-02-02 02:22

I was struggling to fix a code today, then I come across something similar to:

typedef struct {
int a; 
int b; 
int c;
int d;
char* word;
} mystruct;

int main(i         


        
6条回答
  •  太阳男子
    2021-02-02 03:05

    When you allocate memory for structptr, the pointer word in the struct has no valid memory to point. So you either malloc a piece of memory for word, too, or make word point to another character.

提交回复
热议问题