How to define an array of struct nested within a struct?

Discussion in 'C Programming' started by Daniel Rudy, Mar 29, 2006.

  1. Daniel Rudy

    Daniel Rudy Guest

    Is the following code legal?

    typedef stuct data_type_tag {
    int var1;
    int var2;
    int var3;
    struct {
    void *p;
    size_t size;
    unsigned int flags;
    } ptrs[64];
    } data_type_t;


    What I am trying to do is something like this:


    int c, p;
    data_type_t *data;

    int main(void)
    {
    p = 0
    data = malloc(sizeof(data_type_t));
    for (c = 0; c < MAXSIZE; c++)
    {
    data->prts[c].size = p;
    data->ptrs[c].ptr = NULL;
    data->ptrs[c].flags = 0;
    }
    }


    Is that the correct way of building a structure with a format like that?


    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Mar 29, 2006
    #1
    1. Advertising

  2. Daniel Rudy opined:

    > typedef stuct data_type_tag {
    > int var1;
    > int var2;
    > int var3;
    > struct {
    > void *p;
    > size_t size;
    > unsigned int flags;
    > } ptrs[64];
    > } data_type_t;
    >
    >
    > What I am trying to do is something like this:
    >
    >
    > int c, p;
    > data_type_t *data;
    >
    > int main(void)
    > {
    > p = 0
    > data = malloc(sizeof(data_type_t));
    > for (c = 0; c < MAXSIZE; c++)
    > {
    > data->prts[c].size = p;
    > data->ptrs[c].ptr = NULL;
    > data->ptrs[c].flags = 0;
    > }
    > }


    It looks OK. You did re-type instead of copy/paste. Don't do that.

    I'd also suggest you define the inner structure outside, and use that
    in the declaration of the final one. Much cleaner, and very useful if
    you ever need to use it in other places as well.

    --
    BR, Vladimir

    Those aren't WINOS -- that's my JUGGLER, my AERIALIST, my SWORD
    SWALLOWER, and my LATEX NOVELTY SUPPLIER!!
     
    Vladimir S. Oka, Mar 29, 2006
    #2
    1. Advertising

  3. Daniel Rudy

    Daniel Rudy Guest

    At about the time of 3/28/2006 11:04 PM, Vladimir S. Oka stated the
    following:
    > Daniel Rudy opined:
    >
    >> typedef stuct data_type_tag {
    >> int var1;
    >> int var2;
    >> int var3;
    >> struct {
    >> void *p;
    >> size_t size;
    >> unsigned int flags;
    >> } ptrs[64];
    >> } data_type_t;
    >>
    >>
    >> What I am trying to do is something like this:
    >>
    >>
    >> int c, p;
    >> data_type_t *data;
    >>
    >> int main(void)
    >> {
    >> p = 0
    >> data = malloc(sizeof(data_type_t));
    >> for (c = 0; c < MAXSIZE; c++)
    >> {
    >> data->prts[c].size = p;
    >> data->ptrs[c].ptr = NULL;
    >> data->ptrs[c].flags = 0;
    >> }
    >> }

    >
    > It looks OK.


    Thanks. I wasn't sure how it should look, and none of my reference
    materials clearly explained this particular situation.

    > You did re-type instead of copy/paste. Don't do that.


    The code is a simplified example to illustrate what I am trying to do.

    > I'd also suggest you define the inner structure outside, and use that
    > in the declaration of the final one. Much cleaner, and very useful if
    > you ever need to use it in other places as well.



    Thanks for looking it over.

    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Mar 29, 2006
    #3
  4. Daniel Rudy wrote:
    > At about the time of 3/28/2006 11:04 PM, Vladimir S. Oka stated the
    > following:
    > > Daniel Rudy opined:


    <snip>

    >
    > > You did re-type instead of copy/paste. Don't do that.

    >
    > The code is a simplified example to illustrate what I am trying to do.


    Making sure it's syntactically correct makes it easier for someone
    who'd like to copy/paste and run the example. It may not have been as
    important in this case, but will generally increase the number and
    quality of replies to questions.

    <snip>

    > Thanks for looking it over.


    You're welcome. :)

    --
    BR, Vladimir
     
    Vladimir S. Oka, Mar 29, 2006
    #4
  5. Daniel Rudy wrote:
    > Is the following code legal?
    >
    > typedef stuct data_type_tag {
    > int var1;
    > int var2;
    > int var3;
    > struct {
    > void *p;
    > size_t size;
    > unsigned int flags;
    > } ptrs[64];
    > } data_type_t;
    >
    >
    > What I am trying to do is something like this:
    >
    >
    > int c, p;
    > data_type_t *data;
    >
    > int main(void)
    > {
    > p = 0


    I'am afraid you just have missed a ";".
    > data = malloc(sizeof(data_type_t));
    > for (c = 0; c < MAXSIZE; c++)
    > {
    > data->prts[c].size = p;
    > data->ptrs[c].ptr = NULL;
    > data->ptrs[c].flags = 0;
    > }


    Well, it will be better to add a statement "return 0;".
    > }
    >
    >
    > Is that the correct way of building a structure with a format like that?


    OK, basically I agree with your method.
    >
    >
    > --
    > Daniel Rudy
    >
    > Email address has been base64 encoded to reduce spam
    > Decode email address using b64decode or uudecode -m
    >
    > Why geeks like computers: look chat date touch grep make unzip
    > strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Captain Winston, Mar 29, 2006
    #5
  6. Daniel Rudy

    John Bode Guest

    Daniel Rudy wrote:
    > Is the following code legal?
    >
    > typedef stuct data_type_tag {
    > int var1;
    > int var2;
    > int var3;
    > struct {
    > void *p;
    > size_t size;
    > unsigned int flags;
    > } ptrs[64];
    > } data_type_t;
    >


    Perfectly legal, although I'd do

    struct inner {
    void *p;
    size_t size;
    unsigned int flags;
    };

    typedef struct data_type_tag {
    int var1;
    int var2;
    int var3;
    struct inner ptrs[64];
    } data_type_t;

    Keeps things a little cleaner.

    >
    > What I am trying to do is something like this:
    >
    >
    > int c, p;
    > data_type_t *data;
    >
    > int main(void)
    > {
    > p = 0
    > data = malloc(sizeof(data_type_t));


    Personally, I'd write that as

    data = malloc(sizeof *data);

    That way you don't have to worry about syncing the data type with the
    item being allocated.

    > for (c = 0; c < MAXSIZE; c++)
    > {
    > data->prts[c].size = p;
    > data->ptrs[c].ptr = NULL;
    > data->ptrs[c].flags = 0;
    > }
    > }
    >
    >
    > Is that the correct way of building a structure with a format like that?


    Looks okay to me.
     
    John Bode, Mar 29, 2006
    #6
  7. Daniel Rudy

    Guest

    Daniel Rudy wrote:
    > Is the following code legal?
    >
    > typedef stuct data_type_tag {
    > int var1;
    > int var2;
    > int var3;
    > struct {
    > void *p;
    > size_t size;
    > unsigned int flags;
    > } ptrs[64];
    > } data_type_t;
    >
    >
    > What I am trying to do is something like this:
    >
    >
    > int c, p;
    > data_type_t *data;
    >
    > int main(void)
    > {
    > p = 0
    > data = malloc(sizeof(data_type_t));
    > for (c = 0; c < MAXSIZE; c++)
    > {
    > data->prts[c].size = p;
    > data->ptrs[c].ptr = NULL;
    > data->ptrs[c].flags = 0;
    > }
    > }
    >


    It is legal as long as MAXSIZE < 64.

    Karthik
    >
    > Is that the correct way of building a structure with a format like that?
    >
    >
    > --
    > Daniel Rudy
    >
    > Email address has been base64 encoded to reduce spam
    > Decode email address using b64decode or uudecode -m
    >
    > Why geeks like computers: look chat date touch grep make unzip
    > strip view finger mount fcsk more fcsk yes spray umount sleep
     
    , Mar 31, 2006
    #7
  8. Daniel Rudy

    Daniel Rudy Guest

    At about the time of 3/30/2006 9:47 PM, stated the
    following:
    > Daniel Rudy wrote:
    >> Is the following code legal?
    >>
    >> typedef stuct data_type_tag {
    >> int var1;
    >> int var2;
    >> int var3;
    >> struct {
    >> void *p;
    >> size_t size;
    >> unsigned int flags;
    >> } ptrs[64];
    >> } data_type_t;
    >>
    >>
    >> What I am trying to do is something like this:
    >>
    >>
    >> int c, p;
    >> data_type_t *data;
    >>
    >> int main(void)
    >> {
    >> p = 0
    >> data = malloc(sizeof(data_type_t));
    >> for (c = 0; c < MAXSIZE; c++)
    >> {
    >> data->prts[c].size = p;
    >> data->ptrs[c].ptr = NULL;
    >> data->ptrs[c].flags = 0;
    >> }
    >> }
    >>

    >
    > It is legal as long as MAXSIZE < 64.
    >
    > Karthik
    >> Is that the correct way of building a structure with a format like that?
    >>
    >>


    True. Like I said earlier, I wrote this code on the fly as an example.
    I was looking for the basic format of how to get it done.

    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
     
    Daniel Rudy, Mar 31, 2006
    #8
    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. RA Scheltema
    Replies:
    3
    Views:
    410
    RA Scheltema
    Jan 6, 2004
  2. Ultrus
    Replies:
    3
    Views:
    392
    Stefan Behnel
    Jul 9, 2007
  3. Replies:
    3
    Views:
    1,120
  4. TiloVillwock

    struct array within struct

    TiloVillwock, May 15, 2010, in forum: C Programming
    Replies:
    11
    Views:
    727
    Nick Keighley
    May 16, 2010
  5. Tuan  Bui
    Replies:
    14
    Views:
    497
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page