I have to get input on and then be able to print out code using these nested structs in C but I'm getting "error expecting field names. In particular I need to write a string into specific boxes inside of a larger struct containing the boxes. The "sectionNumber" and "boxNumber" bits are me trying to have them separated as items are stored in boxes and each section has can have different numbers of boxes. Number of sections and Number boxes are dynamic. Code snippet follows:
struct Section
{
struct Box * boxes;
int numBoxes;
};
struct Box
{
char Items[1001];
};
. . .
int main(){
struct Section * allSections;
int numSections; //Find No. of sections for mem alloc
int command, sectionNumber, boxNumber; //will be used to discern which command to run
char itemType[1000];
//First input, finding number sections
scanf("%d", &numSections);
//Allocate Memory based on total number of sections
allSections = (struct Section *)malloc(numSections * sizeof(struct Section));
//While loop iterates commands until termination
while (command != 4)
{
scanf("%d", &command);
//Switch to go through commands
switch (command)
{
case 1://Change number of boxes
scanf("%d", §ionNumber);
if (sectionNumber > numSections)
{
break;
}
scanf("%d", &boxNumber);
allSections[sectionNumber].numBoxes = boxNumber;
allSections[sectionNumber].boxes = (struct Box *)malloc((allSections + sectionNumber)->numBoxes * sizeof(struct Box));
break;
case 2://Add to boxes
scanf("%d%d", §ionNumber, &cageNumber);
if (boxNumber >= allSections[sectionNumber].numBoxes)
{
break;
}
scanf("%s", allSections[sectionNumber].boxes[boxNumber].Items); //Seg Fault Here
break;
I'm trying to use something similar to this to read into the boxes but I'm getting segmentation faults in scanf. I had something similar working earlier but I broke the code when trying to recall the stored data in a later section. I'm fairly certain if I can figure out this part then the recalling section should be fairly simple. Like just doing it with a printf over scanf?
Edit: Additional context, variable declarations, allocations, etc. Changed some syntax stuff but still getting segmentation faults. The "something similar" that worked.. I don't remember what I had wrote or I would be much better off than I am now.