Home:ALL Converter>how to allocate memory to a struct that contains a dynamic array? (C)

how to allocate memory to a struct that contains a dynamic array? (C)

Ask Time:2014-04-15T05:05:12         Author:TonyW

Json Formatter

I am not quite clear on how to allocate memory to a struct pointer that contains a dynamic array field. for example, I have the following struct:

typedef struct log_file {
    char *name;
    int updatesize;
    int numupdates;
    int *users;  /* dynamic array of integers */
} log_data;

When I created a pointer of log_data using: log_data *log_ptr = malloc(sizeof(log_data));

How do I allocate enough memory for the dynamic array users in the struct?

Author:TonyW,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23070394/how-to-allocate-memory-to-a-struct-that-contains-a-dynamic-array-c
yy