Thread: structure in array

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    structure in array

    Hi,

    I'm pretty new with the notion of structure and I need some help. I would like to put, for example, the first name, last name, age and the phone number of a person in a structure like:
    Code:
    struct
       {
           char FirstName[16];
           char LastName[16];   
           int age;
           char PhoneNumber[10];
       } person;
    My problem is that I have multiple persons (20 for example). I would like to put them all in one array that would contain the 20 structures. Could someone show me an exemple how to do this when these info are read from the keyboard and, when they are read from a .txt file.

    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To declare an array of structures, just like declaring an array of any other type, you'd use:
    Code:
    struct
       {
           char FirstName[16];
           char LastName[16];   
           int age;
           char PhoneNumber[10];
       } person[20];
    Then you can access the members of person like so:
    Code:
    puts(person[0].FirstName);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by nevrax View Post
    Hi,

    I'm pretty new with the notion of structure and I need some help. I would like to put, for example, the first name, last name, age and the phone number of a person in a structure like:
    Code:
    struct
       {
           char FirstName[16];
           char LastName[16];   
           int age;
           char PhoneNumber[10];
       } person;
    My problem is that I have multiple persons (20 for example). I would like to put them all in one array that would contain the 20 structures. Could someone show me an exemple how to do this when these info are read from the keyboard and, when they are read from a .txt file.

    Thanks
    Your problem is that this code defines an anonymous structure without an identifier and immediately initializes one "person". If you want to reuse the structure, you can write:

    Code:
    struct person {
           char FirstName[16];
           char LastName[16];   
           int age;
           char PhoneNumber[10];
    };
    
    int main(void)
    {
    struct person myPerson1, myPerson2, myPersonArray[20];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM