简体   繁体   中英

how to create nested struct in c

I have started to learn C and I am finding that creating complex data structures can be quite challenging!

Here is the background:

I have a created a struct in a header file foo.h and made its contents public:

struct frame {
     char *name;
     int width;
     int height;
     //other stuffs
}

extern const struct frame
     vid_1080p,
     vid_720p;

The instances of frame are constant and can be accessed from other c flies. foo.c look like this:

const struct frame vid_1080p = {
    .name                 = "1080p",
    .width                = 1920,
    .height               = 1080,
};
const struct frame vid_720p = {
    .name                 = "720p",
    .width                = 1280,
    .height               = 720,
};

I am want to create another struct within struct frame which elements will be calculated at startup of my program and will be possible to modify this if necessary. I am not sure how to approach this, I have tried this approach below and it does not work.

My failed approach:

I have modified foo.h as such:

struct frame_calc {
     int ratio;
     //other stuffs
}

struct frame {
     char *name;
     int width;
     int height;
     //other stuffs
     struct frame_calc *calc;
}

And foo.c is also modified:

 const struct frame vid_1080p = {
        .name                 = "1080p",
        .width                = 1920,
        .height               = 1080,
        .calc                 =  malloc(sizeof(struct frame_calc)) //compiler complains here
    };
    const struct frame 720p = {
        .name                 = "720p",
        .width                = 1280,
        .height               = 720,
        .calc                 =  malloc(sizeof(struct frame_calc))
    };

And then init() is called once at the beginning of my program and it fills out the calc structure:

void init(void)
{
     vid_1080p.calc.ratio = vid_1080p.height / vid_1080p.width;
     vid_720p.calc.ratio  = vid_720p.height  / vid_720p.width;
}

This approach gives me a few compiler errors. I am also not sure how to initialize my nested struct appropriately. Another concern is, I am using malloc , that means I will need to free this at the right places. I would like to avoid this. I am sure all the pro c programmers out there know how to tackle this better!

Last question, how do I access this ratio member for vid_1080p instance from other c files? I am thinking vid_1080p->frame->calc->ratio .

Hopefully, I have managed to explain what I want to do? If not, I would appreciate constructive criticism on how to modify this question better in StackOverflow, given that it is my first question!

You don't need to malloc the calc member because an actual instance is embedded - it is not a pointer.

If for some reason you need it to be a pointer then you need:

struct frame {
     ...
     struct frame_calc* calc;
}

And access would be var.calc->ratio = something;

If you are trying to modify the struct after creation (via init() ) why are the structs const ? Were you trying to get around the const struct issue by making the struct hold a pointer so you wouldn't have to change the pointer but could change the value it points to?

I'd suggest not using const structs:

struct frame vid_1080p {
    ...
}

Your init function can then do vid_1080p.calc.ratio = vid_1080p.height / vid_1080p.width; If you really want to enforce constness access the structs via a pointer to a const struct. const frame *p_1080p .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM