learn through suffering C language learn through suffering 
C 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.

Another way of writing it.
Having an array of structures means it uses pointer variables internally. Therefore, you can also write it in a pointer-like style, beyond what was explained in the previous section. That is, the following three are equivalent:

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

Now, to explain why these three are completely equivalent,
you need a solid understanding of how structures and pointers work.
Do your teachers or supervisors understand the difference between these three?

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("[Grade]:%d\n", data[i].year);
        printf("[Class]:%d\n", data[i].clas);
        printf("[Seat 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, 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. line break
  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

Loading comment system...