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

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1

    Initialize array of structs


    I tried initializing an array of structs using a for loop, but for some reason, every time i try to change a new struct, it initializes all the fields of every struct in the array.

    for example:

    typedef struct student
    {
    char *name;
    char *age;
    char *class;

    }student;

    struct student stu_array[100];

    for (i=0;i<100;i++)
    {
    scanf("%s ",&name);
    scanf("%s ",&age);
    scanf("%s",&class);

    stu_array[i].name=name;
    stu_array[i].age=age;
    stu_array[i].class=class;
    }

    if at first i insert 'joe,22,03' to stu_array[0]
    and then 'bob,24,01' to stu_array[1]
    and try to print, both will be: 'bob,24,01'

    what am I doing wrong?

    Thnaks in advance...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Where do you allocate memory for those pointers that are contained in your structure?

    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1
    I thought that this line:
    struct student stu_array[100];
    saves room for 100 structs in the size of student in the stack.
    is it wrong?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Yes it does, but that doesn't allocate any memory for those pointers that are contained within that structure.

    Perhaps something like the following would make more sense.

    Code:
    typedef struct student
    {
       char name[50];
       char age[10];
       char class[50];
    }student;

    If you don't statically allocate the size of your strings you'll need to use malloc() to allocate memory for the strings. Allocating space for the structure doesn't allocate any memory for the strings, you will still need to do the allocations.

    Jim
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Also, your scanf's are wrong:
    Code:
            scanf("%s ",&name);
            scanf("%s ",&age);
            scanf("%s",&class);
    scanf needs an address. A pointer is already an address, so giving it the address of the pointer makes absolutely no sense. Basic rule: when you are passing scanf a pointer or an array name (essentially the same thing), do not use an ampersand.

    You also did not use code tags, so you code listing lost its indenting and was rendered unreadable. Always use CODE TAGS to post code listings!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1
    And then when I assign the new parameters, how should I write it?
    now its:
    stu_array[i].name=name;

    should I assign it to the first position? like this: stu_array[i].name[0]=name; ?

    thanks..
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1
    Originally Posted by dwise1_aol
    Also, your scanf's are wrong:
    Code:
            scanf("%s ",&name);
            scanf("%s ",&age);
            scanf("%s",&class);
    scanf needs an address. A pointer is already an address, so giving it the address of the pointer makes absolutely no sense. Basic rule: when you are passing scanf a pointer or an array name (essentially the same thing), do not use an ampersand.

    You also did not use code tags, so you code listing lost its indenting and was rendered unreadable. Always use CODE TAGS to post code listings!
    It's actually not the original code...
    The original is long and messy...
    I'm new here, didn't know the option of code tags.
    thanks
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    And then when I assign the new parameters, how should I write it?
    Why not just use the scanf()?
    Code:
    scanf("%s", stu_array[i].name);
    Otherwise you'll need to use strcpy().

    Jim
    Last edited by jimblumberg; January 7th, 2014 at 12:16 PM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    1
    Thanks Jim.
    I can't use scanf because I get a single string with all the info and split it with strtok.

    used strcpy, worked like a charm :)

IMN logo majestic logo threadwatch logo seochat tools logo