MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Array of Structures

Array of Structures
Structures can also be arrays.The method is the same as it has always been.
Here's an example of declaring an array of structure variables, each of type student, with 10 elements.

Array of structure variables
student data[10];

The usage is exactly the same as previous arrays.
Here's an example of accessing struct array elements using an index.

Accessing elements of a structure array
data[1].year = 3;
strcpy(data[1].name, "MARIO");

The other aspects are the same as with the previous array usage.

Alternative phrasing
The fact that you have an array of structures means it's using pointer variables internally.
Therefore, it can also be written in a way other than what was explained in the previous section, such as in a pointer-variable-like manner.
In other words, the following three are equivalent.

(*data).year
data->year
data[0].year

ところで、この3つがまったく同じ意味であることをDescriptionするには、
You need to thoroughly understand how structures and pointers work.
Do your teachers and supervisors understand these three differences?

Structure array arguments
You can also pass a structure array as an argument to a function.
In that case, it would be like passing it as an array.
"It essentially passes the address of the first element of the structure array."

You can treat the received function just like the array arguments it replaces.
The following function displays the contents of the student type a specified number of times.

Display a specified number of student records.
void student_print(student data[], int count)
{
    int i;
    
    for (i = 0; i < count; i++)
    {
        printf("[学year]:%d\n", data[i].year);
        printf("[クラス]:%d\n", data[i].clas);
        printf("[出席番号]:%d\n", data[i].number);
        printf("[名前]:%s\n", data[i].name);
        printf("[身長]:%f\n", data[i].stature);
        printf("[体重]:%f\n", data[i].weight);
    }

    return;
}

In this case, it will behave as if an array were passed.
If you modify the contents of a structure variable where it's passed, the caller will also be modified.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19