[Solved] Using Malloc() to create an integer array using pointer [closed]


It can be inferred from the error message, intarr_create(): null pointer in the structure's data field, that data fields of each struct are expected to be allocated.

intarr_t* intarr_create(size_t len){
    intarr_t* ia = malloc(sizeof(intarr_t) * len);
    size_t i;
    for(i = 0; i < len; i++)
    {
        // ia[len].len = 0; // You can initialise the len field if you want
        ia[len].data = malloc(sizeof(int) * 80); // 80 just for example
        if (ia[len].data == 0)
        {
            fputs("Warning: failed to allocate memory for an image structure", stderr); 
            return 0;
        }
    }
    return ia; // Check whether the return value is 0 in the caller function
}

6

solved Using Malloc() to create an integer array using pointer [closed]