Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Array of structures

Options
  • 28-04-2009 1: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 Posts: 1,916 ✭✭✭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 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