C Pointer becomes NULL in a loop
I found many questions "pointer becomes NULL", but struggling to apply the answers to my problem.
I have 3 nested structures. Struct A
must contain an array struct B
, which contains one structure C
, which itself must contain an array int
:
typedef struct {
int32_t *data;
uint16_t nData;
uint16_t dataOffset;
} Window;
typdef struct {
Window *window;
const char *datasetPath;
} Dataset
typedef struct {
char *id;
uint8_t count;
Dataset *datasets;
} DatasetGroup;
I have a function new
for structs:
int8_t dataset_new(Dataset *ds, const char *datasetPath, uint16_t winSize){
ds->datasetPath = datasetPath;
ds->window = malloc(sizeof(*(ds->window));
ds->window->data = malloc(sizeof(int32_t) * (winSize));
return 0;
int8_t datasetgroup_new(DatasetGroup *dg, char *id){
dg->id = id;
dg->count = 0;
dg->datasets = malloc(sizeof(*(dg->datasets)) * 255);
}
And I have a function add
to add Dataset
to DatasetGroup
:
int8_t datasetgroup_add(DatasetGroup *dg, const char *filePath, uint16_t winSize){
// Create the dataset
Dataset ds;
dataset_new(&ds, filePath, winSize);
// Add the dataset to the dataset array
dg->datasets[dg->count] = ds;
dg->count++;
return 0;
}
Then I iterate over the data through the dataset to populate the data, doing things like:
for (i = 0 ; i < datasetCount ; i++){
Dataset *ds = &(dg->datasets[i])
Always in the second introduction the array data
becomes a null pointer: data = ds-> window-> data p>
I understand that I did something wrong by missing pointers (?), But I'm not sure what exactly I did wrong ....
source to share