Thread: Structs in C

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    12

    Structs in C

    Hi,
    I am trying to do the following:
    Code:
    typedef struct _STRUCT1
    {
            integer var1[SIZE];
    
    } STRUCT1
    
    typedef struct _STRUCT2
    {
           STRUCT1 var2(100);
           STRUCT1 var3(200);
    } STRUCT2
    I want that var2.var1 will have the array size=100
    and var3.var1 will have the array size=200;
    is it posible, or must i do this:

    Code:
    typedef struct _STRUCT1
    {
            integer var1[100];
    
    } STRUCT1
    
    typedef struct _STRUCT3
    {
            integer var1[200];
    
    } STRUCT3
    
    typedef struct _STRUCT2
    {
           STRUCT1 var2;
           STRUCT3 var3;
    } STRUCT2

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    have you compiled and see it yourself? tho i saw some problems with your struct definition

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    typedef int hun1[100];
    typedef int hun2[200];
    
    typedef struct _MYSTRUCT
    {
        hun1 var1;
        hun2 var2;
    } MYSTRUCT;

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    obvious that It's not working..
    it was just an hypothetical example.

    I am trying to do this now:

    Code:
    #define MACRO1(_SIZE,_NAME)    struct _NAME        \
                                                         {                                        \
                                                               INT32   var1[_SIZE];    \
                                                          }; 
    
    
    typedef struct _STRUCT2
    {
           MACRO1(100,var2)
           MACRO1(200,var3)
    } STRUCT2

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    maybe struct templates?
    any ideas?

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #define MACRO1(_SIZE,_NAME) int _NAME[_SIZE];
    
    typedef struct _STRUCT2
    {
        MACRO1(100,var2)
        MACRO1(200,var3)
    } STRUCT2;

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    its just not possible to have 1 type representing 2 different sizes. other means are needed here.
    What would sizeof(struct _NAME) be?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at the macro again. "_NAME" isn't a struct. It's the name of the variable you want your array to be called. Were I to use this, I'd probably reverse the arguments, but that's just me.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    Look at the macro again. "_NAME" isn't a struct. It's the name of the variable you want your array to be called. Were I to use this, I'd probably reverse the arguments, but that's just me.


    Quzah.
    ah, I was responding to amitbern's post where _NAME is a struct. Theres many options to do this, but having 1 struct with 2 different sizes is just not one of them.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    When you define a structure, you need to define all the variables in it with all data sizes.
    So if you have already defined a structure, you can't just cast it with different sizes. It's already defined.

    If you're are going to have only one variable in the struct, think again. Why do you need struct then?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by maxorator
    When you define a structure, you need to define all the variables in it with all data sizes.
    No you don't. The last object in a structure is allowed to be an array of undefined size. The size field of the array is left blank. This is known as a flexible array member. It is allowed as of C99. However, it has its own rules, such as, said structure can not be part of an array.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM