1. ## Array of records?

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.

2. 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...

3. 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!

4. 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);
}```

5. 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}
};```

6. //patient[i].lastname = "Smith";
to copy C-strings you are using strcpy function

7. 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;```