Structs in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amitbern
    New Member
    • Oct 2006
    • 6

    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
  • pedz
    New Member
    • Oct 2006
    • 4

    #2
    Someone please check my syntax but something like:

    Code:
    template <int size>
    struct _STRUCT1
    {
        integer var1[size];
    };
    Now you can do a couple of things. One is:
    Code:
    typedef _STRUCT1<100> s100;
    typedef _STRUCT1<200> s200;
    
    struct foo {
        s100 s1;
        s200 s2;
    };
    or you can put the use of the template inside the struct like:
    Code:
    struct foo {
        _STRUCT1<100> s1;
        _STRUCT1<200> s2;
    };

    Comment

    • amitbern
      New Member
      • Oct 2006
      • 6

      #3
      templates in C?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by amitbern
        templates in C?
        No, I suspect pedz was thinking in C++.

        "Think in C" to paraphase Firefox.

        Anyway you can't do what you have as your first example you will have to do what you gave as your second example or alternatively put the arrays directly into STRUCT2

        Code:
        typedef struct _STRUCT2
        {
               int var1[100];
               int var2[200];
        } STRUCT2

        Comment

        Working...