Array of records?

This is a discussion on Array of records? within the C Programming forums, part of the General Programming Boards category; Also see post #3 please! Hi guys, I'm currently learning C, and I've been following this online guide. I've gotten ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Array of records?

    Also see post #3 please!

    Hi guys, I'm currently learning C, and I've been following this online guide. I've gotten up to one of the many (Try This!) bubbles, and I'm stuck as to what it really means.

    It reads: "Create an array of records and write some code to sort that array on one integer field."

    I understand a bit about arrays and how they work, but I'm not quiet sure what it means by 'an array of records'. Not sure what records are, as it has no mention so far in the guide. Another thing, 'write some code to sort that array on one integer field', what does it mean by integer field.

    In the code just before the example it says something about 'struct rec r;' and has something like:

    Code:
    struct rec
    {
    int a,b,c;
    float d,e,f;
    } r;
    So maybe it's reffering to something like that? I really am confused here lol.

    EDIT: It also says "struct rec r[50]; /* Array of records*/

    So yeah.. Thanks.

    Btw I just found out this guide is like years and years old, so maybe if someone knows of a more up to date one that'd help out. Thanks.

    Also see post #3 please!
    Last edited by pobri19; 05-03-2008 at 01:22 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    struct rec
    {
    int a,b,c;
    float d,e,f;
    }
    this is a user difined type
    it is a struct containing 6 fields - 3 integers and 3 floats

    this struct represents some user record, hard to say what exactly due to these nice names used - but you need to use your imagination
    say a - is a day
    b is a month
    c is a year
    d is a weight
    e is a temperature
    f is a hair length

    alltogether they represent some measurments done on a patient for example...

    now you have 50 measurments - you declare the array of records
    Code:
    struct rec r[50];
    initialize this array in some way - and want to sort it based on one integer field - say c...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    OK uhh I'm still pretty confused but I think I understand what you're getting at.

    The only problem is this guide doesn't say how exactly I'm supposed to do any of that (as in how to format everything correctly) and where I need to put the code, basically, I have no idea how I'm supposed to be setting the values inside the records to values/strings.

    I'm guessing what it wants me to do is make an array of 5 or so structures (struct rec records[5], each array would contain information of 1 person. I.e records[0] would contain 'char firstname[20], char lastname[20], int yearborn', for 1 specific person, then records[1] would contain the same variables as records[0] and same with all the other records[#] but the values for each variable in array number would be set differently? Then I'm supposed to print all of the structures, but in order judging on an integer (which would be the 'int yearborn')? Or maybe I'm supposed to just print the variables inside the structures in order, not the actual structures? Or am I not making sense..

    As you can probably tell I'm still fairly confused, and I'm finding it difficult to try and explain what I'm thinking. If anyone could help me to understand this thing that'd be great haha, thanks.

    EDIT: here's an example of what I'm trying to say (it might be pretty off so yeah):

    Oh and do structures go inside functions? Or are structures considered functions, if so then I assume I need to call/declare the structure?

    Code:
    struct rec records[5];
    
    struct rec
    {
    char firstname[20], lastname[20];
    int yearborn;
    } records[a];
    And the variable 'a' would have a loop for it to set all 5 structures with the same variables?
    Then I would set for instance, records[0] firstname to something, same with lastname and yearborn. And then the same again for records[1] with different values for firstname, lastname, yearborn? Then make something to sort the values? Also as you can probably tell I'm unsure how to set the values of a variable inside a structure :P.

    OK thanks, hopefully I didn't confuse the hell out of you!
    Last edited by pobri19; 05-03-2008 at 01:21 AM.

  4. #4
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    This is pretty much how you use a array of records. Declare the type and make a variable of that type. Using a for loop fill each record one at a time. In this example I have just set yearborn, but you would obviously want to set the others.

    Code:
    struct records
    {
    char firstname[20], lastname[20];
    int yearborn;
    };
    
        struct records patient[5];
        int i;
      for(i=0; i<5; i++) {
              patient[i].yearborn = 1967;
              //patient[i].lastname = "Smith";
      }
        for(i=0; i<5; i++) {
              printf("%i\n", patient[i].yearborn);
      }
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    The only problem is this guide doesn't say how exactly I'm supposed to do any of that
    it is a life. You are told what need to be done - it is upto you to decide how it is to be done...

    I have no idea how I'm supposed to be setting the values inside the records to values/strings.
    to initialize array of structs you can use
    Code:
    struct test
    {
       char name[20];
       int age;
    } person[3] = 
      {
          {"Nick", 20},
          {"Rick", 16},
          {"Ann", 14}
      };
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    //patient[i].lastname = "Smith";
    to copy C-strings you are using strcpy function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    If you want to reuse the structure in other parts of your code, do this:

    Code:
    typedef struct Record
    {
      char firstname[20], lastname[20];
      int yearborn;
    };
    
    Record myRecords[20];
    myRecords[0].yearborn = 2;
    Otherwise you can do it like this:

    Code:
    struct
    {
      char firstname[20], lastname[20];
      int yearborn;
    } myRecords[20];
    
    myRecords[0].yearborn = 2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21