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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Array of structures

  • 28-04-2009 12: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,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, 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