Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Array of structures

  • 28-04-2009 01:19AM
    #1
    Closed Accounts Posts: 29


    Trying to create an array of structs that stores 4 peoples info and prints it out in a loop:
    struct Person{
             char name[20];
             int age;
             float height;
             };
             
             
             struct Person beatle[4];
    

    I know I created 4 instances with beatle[4]; but I'm not sure how to edit the information individualy

    i.e struct Person beatle[1] = {"John Lennon"};
    Thanks and strictly C only


Comments

  • Registered Users, Registered Users 2 Posts: 1,919 ✭✭✭ronivek


    Well there's two primary ways depending on whether you're using a pointer or not as follows;
    struct Person p1;
    struct Person * p2ptr;
    
    ...
    
    p1.age = 32;
    p1.height = 3.43;
    
    p2ptr->age = 21;
    p2ptr->height = 3.2;
    

    EDIT: And using an array as you are above you use the following...
    beatle[0].age = 32;
    beatle[1].heigh = 4.2;
    


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    ^^^^ as above and also if you wanted to initialise you can try
    e.g.
    Person beatle[1] = {"John Lennon", 44, 6.0};


Advertisement