#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Struct array in c


    hello
    does anyone knows how can i create an array of structs and how can i get every struct address?
    actually i dont know the size of the array and i tried something like
    structname *example;
    but it doesnt work.
    tnx
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,403
    Rep Power
    1871
    Did you try
    Code:
    // an array of 10 structname's
    structname example[10];
    > and how can i get every struct address?
    That would be &example[index] for all the valid subscripts of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    France
    Posts
    2
    Rep Power
    0
    Hi,

    actually i dont know the size of the array
    So structname example[10] isn't appropriated!

    structname *example; may work, but it depends on the way you use it! If you don't know the size, you have to allocate it with malloc(). After that, you use it like salem showed you : &example[index].
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,214
    Rep Power
    2222
    Originally Posted by comi
    hello
    does anyone knows how can i create an array of structs and how can i get every struct address?
    actually i dont know the size of the array and i tried something like
    structname *example;
    but it doesnt work.
    tnx
    structname *example would serve to point to the dynamically created array, but it is useless until you have initialized it with memory to contain that array. Since this is C, you would use malloc thus:
    example = malloc(sizeof(structname)*ARRAY_SIZE);
    where ARRAY_SIZE would be a macro (AKA "a #define") representing the number of elements in the array.

    You don't know what size to make the array, so malloc it first with some arbitrary size. I would use 10 just because it's a not-too-big round number, but you could even use 1. Then when you have filled that array and need to add more, use realloc() to change the size of the array. You could add one more element with each call to realloc, but I would think that that operation would slow your program down a bit, so I would be inclined to add multiple elements each time, like say another 10. Or you could just add one at a time and see how well it works.

    You should also be keeping in an integer variable a count of the number of structs you have in that array.

    PS

    Merci, schadocalex! I didn't see where he said that he doesn't know ahead of time how many structs he'll have to store in the array.
    Last edited by dwise1_aol; February 11th, 2013 at 02:11 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    France
    Posts
    2
    Rep Power
    0
    Sorry, i understood it like that.

    comi -> If you use realloc(), be sure to know how to use it! If realloc() fail (for any reason) and you don't operate (is the good verb? lol) it, you can say hello to difficulties, which are different from malloc().

IMN logo majestic logo threadwatch logo seochat tools logo