Writing to a field inside of nested Structs in C where structs are defined dynamically

Viewed 52

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", &sectionNumber);
    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", &sectionNumber, &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.

1 Answers

I would not use pointers only flexible array members.

typedef struct Box
{
    char Items[1001];
}box_t;

typedef struct Section
{
    size_t numBoxes;
    box_t boxes[];
}section_t;


section_t *addBox(section_t *sec, char *Items)
{
    size_t newsize = sec ? sec -> numBoxes + 1 : 1;

    if((sec = realloc(sec, newsize * sizeof(sec -> boxes[0]) + sizeof(*sec))))
    {
        sec -> numBoxes = newsize;
        strcpy(sec -> boxes[newsize - 1].Items, Items);
    } 
    return sec;
}

section_t *removeBox(section_t *sec, size_t box)
{
    if(sec && sec -> numBoxes && box < sec -> numBoxes)
    {
        if((sec = realloc(sec, sec -> numBoxes * sizeof(sec -> boxes[0]) + sizeof(*sec))))
        {
            sec -> numBoxes -= 1;
        } 
    }
    return sec;
}
Related