C language learned by suffering
C language learned by suffering
Array of structures
Array of structures
Structures can also be arrays. The method is the same as before.
The following is an example of declaring an array of structure variables of type student with 10 elements.
The usage is exactly the same as the previous array.
The following is an example of accessing an element of the structure array by specifying the element number.
Other points are the same as in the previous use of arrays.
The following is an example of declaring an array of structure variables of type student with 10 elements.
Array of structure variables
student data[10];
The usage is exactly the same as the previous array.
The following is an example of accessing an element of the structure array by specifying the element number.
Access elements of structure array
data[1].year = 3;
strcpy(data[1].name, "MARIO");
Other points are the same as in the previous use of arrays.
Another way to write
The fact that there is an array of structures means that it uses pointer variables internally.
Therefore, it can be written in a way other than that described in the previous section, i.e., pointer variable style.
In other words, the following three have the same meaning.
(*data).year
data->year
data[0].year
By the way, to explain that these three mean exactly the same thing
You must have a solid understanding of how structs and pointers work.
Do your teachers and supervisors know the difference between the three?
Therefore, it can be written in a way other than that described in the previous section, i.e., pointer variable style.
In other words, the following three have the same meaning.
(*data).year
data->year
data[0].year
By the way, to explain that these three mean exactly the same thing
You must have a solid understanding of how structs and pointers work.
Do your teachers and supervisors know the difference between the three?
Arguments of the structure array
You can also pass a structure array to the function as an argument, but the
In that case, it is passed like an array so far.
That is, the address of the first element of the structure array is passed.
The following function displays the contents of the STUDENT type for the specified number of students.
In this case, the behavior is the same as when an array is passed.
If the destination changes the contents of the structure variable, the caller is also changed.
In that case, it is passed like an array so far.
That is, the address of the first element of the structure array is passed.
The received function can be handled in the same way as the array arguments up to now.
The following function displays the contents of the STUDENT type for the specified number of students.
Display the content of the specified number of STUDENT types
void student_print(student data[], int count)
{
int i;
for (i = 0; i < count; i++)
{
printf("[grade]:%d\n", data[i].year);
printf("[class]:%d\n", data[i].class);
printf("[Attendance number]:%d\n", data[i].number);
printf("[name]:%s\n", data[i].name);
printf("[height]:%f\n", data[i].stature);
printf("[weight]:%f\n", data[i].weight);
}
return;
}
In this case, the behavior is the same as when an array is passed.
If the destination changes the contents of the structure variable, the caller is also changed.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.