malloc dynamic array in dynamic array of structs

前端 未结 3 1019
情书的邮戳
情书的邮戳 2021-01-26 00:30
typedef struct {
    char *word;
} STR;

int main()
{
    STR *arr=(STR*)malloc(5*sizeof(*arr));
    STR[2].word=(char*)malloc(200*sizeof(char));
    STR[2].word=(char*)         


        
3条回答
  •  执笔经年
    2021-01-26 01:14

    This seems redundant. But, if you are trying to create an array of strings using your structure, where the third element is malloc'd as a 199 character string and then realloced as a 399 character string, you would do it like so:

    STR *arr = (STR*)malloc(5*sizeof(STR));
    arr[2].word = (char*)malloc(200*sizeof(char));
    arr[2].word = (char*)realloc(arr[2].word,400*sizeof(char));
    

提交回复
热议问题