Hi, I want to use an array of structs where each struct can have an array of integers. I can set the values of everything ok, but when I try to access the values I get a crash and 'File error! -1' displayed on the screen.
for (int i=0; i<NUM_OF_SENSORS; i++) {
for (int j=0; j<1; j++) {
NumOut(0,0,sensorHandlers[i].cellTargetIds[0]); //THIS LINE CAUSES THE PROGRAM CRASH
}
}
I have also tried assigning the cellTargetIds[0] to an int variable with the same result. Any ideas anybody? Has anyone used structs like these successfully?
Complex data structures such as arrays of structs that contain arrays has been known to result in peculiar errors such as this. However, in this case, the problem is probably caused by the fact that you are initializing your array with a struct instance that has no members in its nested array. NXC does not currently automatically size arrays in structure declarations. It ignores the size in the declaration. So you need to initialize it manually using ArrayInit or by assigning an already allocated array to the struct member.
Try initializing the emptyHandler struct's array before you use it to initialize the array of structs.
SensorHandler emptyHandler;
ArrayInit(emptyHandler, 0, 2); // put 2 zeros into the struct that we use to initialize our array
ArrayInit(sensorHandlers,emptyHandler,NUM_OF_SENSORS);
I do not know how the code that you use to inialize the array could execute without errors.
SensorHandler emptyHandler;
ArrayInit(emptyHandler.cellTargetIds, 0, 2); // put 2 zeros into the struct that we use to initialize our array
ArrayInit(sensorHandlers,emptyHandler,NUM_OF_SENSORS);