invalid use of flexible array -flexible struct array as a member of another struct

前端 未结 2 1527
滥情空心
滥情空心 2021-02-06 00:29

I\'m beginning to learn about the use of structs in C. It\'s challenging and enjoyable. Needless to say I\'ve encountered a problem I can\'t seem to figure out. I\'m trying to m

相关标签:
2条回答
  • 2021-02-06 01:23

    I am not an expert in this C feature but my common sense tells me that you cannot define objects of the type struct enginestruct, only pointers. This regards the engine variable in the following line:

    struct enginestruct engine,*engineptr;
    
    0 讨论(0)
  • 2021-02-06 01:33

    Edit

    I think I remember now what the problem is. When you declare a struct with a flexible array as it's last member it's doing something completely different than what you think.

    struct channelStruct channels[];
    

    is NOT a pointer, it is an in place array which is contiguous with the struct.

    The way this is intended to be used is to lay the struct over an existing block memory. For instance, this is useful in networking when you have a packet with variable length data. So you might do something like:

    struct mydata {
        // various other data fields
        int varDataSize;
        char data[];
    }
    

    When you receive a packet you cast a pointer to the data to a mydata pointer and then the varDataSize field tells you how much you've got. Like I said, the thing to remember is that it's all one contiguous block of memory and data is NOT a pointer.

    Old Answer:

    I think that's only allow in the C99 standard. Try compiling with the -std=c99 flag.

    Also, see this thread, Variable array in struct

    Also see this SO post: Flexible array members in C - bad?

    0 讨论(0)
提交回复
热议问题