MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

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.

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?

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

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Division
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box