Complex static memory structures and content

Discussion in 'C Programming' started by Andreas Lundgren, Sep 15, 2008.

  1. Hi!

    I want to put some values in static memory, and then be able to
    reference this data from my code in a decent way by referencing
    AllData.Data. (All code beneth is placed outside functions.)

    ___TYPE___
    typedef struct Data_s
    {
    char* Name_p;
    void* Class_p;
    int Size
    } Data_t;

    ___DATA___
    {"foo", NULL, 0},
    {"bar", NULL, 0},



    I first tried the following, but when compiling for ARM compiler I've
    got "incomplete type is not allowed"

    typedef struct AllData_s
    {
    Data_t Data[];
    } AllData_t;

    static AllData_t AllData = {
    {"foo", NULL, 0},
    {"bar", NULL, 0}
    };



    I then tried the following, but got the same error.

    typedef struct Data_t AllData_t[2];

    static AllData_t AllData = {
    {"foo", NULL, 0},
    {"bar", NULL, 0},
    };



    Finally this worked:
    typedef struct Data_t AllData_t[2];

    static Data_t AllData[2] = {
    {"foo", NULL, 0},
    {"bar", NULL, 0},
    };


    Is is possible to build up more advanced static structures? When I've
    got this to work, I directly use the AllData and not the
    AllData.Data that I really desired. Would it be possible to do a
    more complex data structure at compile time?

    Best Regards,
    Andreas L - Sweden
     
    Andreas Lundgren, Sep 15, 2008
    #1
    1. Advertising

  2. Andreas Lundgren <> writes:

    > I want to put some values in static memory, and then be able to
    > reference this data from my code in a decent way by referencing
    > AllData.Data. (All code beneth is placed outside functions.)
    >
    > typedef struct Data_s
    > {
    > char* Name_p;
    > void* Class_p;
    > int Size


    (Missing ;)

    > } Data_t;
    >

    <snip>
    > Finally this worked:
    > typedef struct Data_t AllData_t[2];
    >
    > static Data_t AllData[2] = {
    > {"foo", NULL, 0},
    > {"bar", NULL, 0},
    > };


    The typedef of AllData_t is not involved (that is why it works!).
    This is the correct way to proceed, but you don't need the 2:

    static Data_t AllData[] = {
    {"foo", NULL, 0},
    {"bar", NULL, 0},
    };

    is fine.

    > Is is possible to build up more advanced static structures?


    Yes. Just keep building it up from the simple piece to the more
    complex ones. If you can use C99 you get even more options, but I
    have not shown anything here that is not "old" C.

    > When I've
    > got this to work, I directly use the AllData and not the
    > AllData.Data that I really desired. Would it be possible to do a
    > more complex data structure at compile time?


    If you now want that array of two Data_t objects to be part of bigger
    struct, then use a pointer:

    struct AllData_s {
    Data_t *Data;
    size_t n_data_objects;
    /* other members ... */
    } all_data = {
    AllData,
    sizeof AllData / sizeof AllData[0]
    };

    Now you can use all_data.Data[1] if that is what you need (for some
    reason).

    --
    Ben.
     
    Ben Bacarisse, Sep 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hazz
    Replies:
    6
    Views:
    49,765
    SkyUCHC
    Jun 9, 2010
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    763
    Stanimir Stamenkov
    Oct 25, 2005
  3. tweak
    Replies:
    14
    Views:
    2,796
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    728
    Emmanuel Delahaye
    Sep 24, 2005
  5. Henry Law
    Replies:
    4
    Views:
    130
    Anno Siegel
    Sep 9, 2005
Loading...

Share This Page