Allocate memory for a struct with a character pointer in C

后端 未结 6 424
礼貌的吻别
礼貌的吻别 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 02:47

    Add a second malloc for whatever length (N) you need for word

       mystruct* structptr = malloc(sizeof(mystruct));
    
       structptr->word = malloc(sizeof(char) * N);
    

提交回复
热议问题