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

Putting together variables of different types

When you want to handle data in bulk
Chapter 13 described arrays, which group together multiple variables of the same type.
In some cases, however, you may want to handle different types together.
Also, it is sometimes more convenient to distinguish each element by name rather than by number.

For example, if you want to store the results of a student's physical measurements at school, you can use the
For each student, data such as class, grade, attendance number, name, height, weight, etc. are needed.
If we were to prepare this in a straightforward manner using variables, it would look something like this

source code
 int year; /* school year */
int clas; /* class */
int number; /* attendance number */
char name[64]; /* name */
double stature; /* height */
double weight; /* weight */


clas?
The correct spelling of clas is class.
However, the name "class" cannot be used, so we use "clas" instead.

This is because most C compilers support C++ as well, but do not support
C++ has a feature called classes, and classes are used for that feature.
In other words, class is a reserved word in C++, so it cannot be used for names.
If you don't like clas, you can use group.

Height and weight are in real number type because they are often expressed to one decimal place.

Naturally, this still works well as student data.
But even though these data are all related
Each one is declared as a separate variable, so it is not very easy to understand.

In such cases, the following methods can be used to combine variables of different types into a single variable
structure is provided.

Keywords.
[Structure

A type made by grouping several different types together.


Structures allow the creation of new types by grouping multiple types together.
For example, to create student data as a structure of a new type, do the following

structure
 struct student
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
};

When declaring a structure type, prefix it with struct.
Next to that is the type name of the newly declared structure under the same rules as for variables.
This type name is sometimes called a structure tag name.

Keyword
[Structure tag name

The name of the created structure itself.
Since it is not a type name, it is handled slightly differently.


In this case, we have given the name student, which means student.
After that, declare the type and name of the variable you want to put together inside the {}.

In this way, you can create a new structure type, but you can also use the
By itself, this merely declares the type and cannot be used in practice.
To actually use it, you must declare a variable of the structure type.
To declare a variable of a structure type, do the following

Declare variables of type structure
 struct student data;

This example declares the data structure variable in the student structure tag.
To declare a variable in a structure tag, prefix it with struct.
Next, the type name of the structure, and finally, the name of the structure variable.
From now on, structure types will be referred to as structure tags and variables declared with structure tags as structure variables.

In C++, the
In C++, which is an extended version of C
You can declare structure variables without adding struct.
Since most current compilers are for C++, you can use
In most cases, it works without struct.

In this way, structure tags and structure variables can be declared.
The following program is an example of declaring structure tags and structure variables.

Declare structure tags and structure variables
 struct student
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
};

int main(void)
{
    struct student data;
    return 0;
}

As in this example, structure tags are usually declared before functions.
This is because then the structure can be used in all functions that follow.
How to use the structure
In the previous section, we discussed structure tag and structure variable declarations.
Now that we've declared the structure, let's get to using it!

Structure variables have all the types that were declared in the original structure tag.
With arrays, we used to distinguish variables of the same type by number.
With structure variables, all elements are distinguished by name, regardless of type.
To access a single element held by a structure variable, do the following

Access one element of a structure variable
 Structure Variable Name. Element name

where . uses the decimal point symbol. It is not a comma.
However, it is just an appropriation of the decimal point symbol and has nothing to do with the minority itself.
It's called pointers. C language has a bad habit of appropriating unrelated symbols...

Individual elements can be accessed in this way.
The following program is an example of using the year element of the previous structure.

Source Code
 #include <stdio.h>

struct student
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
};

int main(void)
{
    struct student data;

    data.year = 10; /* access year element */
    printf("%d\n", data.year);

    return 0;
}

The result of executing this program is as follows

Execution Result
10

This example uses the year element of the data structure variable in the student structure tag.
When accessed in this way, it is now exactly the same as an ordinary variable.
You can also access arrays in the same way.
The following program is an example of accessing the name element of the previous structure.

source code
 #include <stdio.h>
#include <string.h>

struct student
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
};

int main(void)
{
    struct student data;

    strcpy(data.name, "MARIO");
    printf("%s\n", data.name);

    return 0;
}

The result of executing this program is as follows

Execution Result
MARIO.

Of course, it is also possible to access each element of the array with [].
Processing of the structure variable itself
In the previous section, we described how to access each element of the structure
While this certainly looks cohesive on the outside, it is not.
The actual usage is exactly the same as for ordinary variables, which does not seem to make much sense.

In the case of structures, however, structure variables themselves can be treated as variables.
For example, structure variables can be assigned to each other.
The following program is an example of structure variable-to-structure assignment.

source code
 #include <stdio.h>
#include <string.h>

struct student
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
};

int main(void)
{
    struct student data1, data2;

    /* Assign to data1 */
    data1.year = 3;
    data1.class = 4;
    data1.number = 18;
    strcpy(data1.name, "MARIO");
    data1.stature = 168.2;
    data1.weight = 72.4;

    data2 = data1; /* copy the contents of data1 to data2 */

    /* display contents of data1 and data2 */
    printf("data1.year = %d : data2.year = %d\n", data1.year, data2.year);
    printf("data1.clas = %d : data2.clas = %d\n", data1.clas, data2.clas);
    printf("data1.number = %d : data2.number = %d\n", data1.number, data2.number);
    printf("data1.name = %s : data2.name = %s\n", data1.name, data2.name);
    printf("data1.stature = %f : data2.stature = %f\n", data1.stature, data2.stature);
    printf("data1.weight = %f : data2.weight = %f\n", data1.weight, data2.weight);

    return 0;
}

The result of executing this program is as follows

Execution Result
data1.year = 3 : data2.year = 3
data1.clas = 4 : data2.clas = 4
data1.number = 18 : data2.number = 18
data1.name = MARIO : data2.year = MARIO
data1.stature = 168.200000 : data2.stature = 168.200000
data1.weight = 72.400000 : data2.weight = 72.400000

In this program, we first make an assignment to each element of data1.
Then, data1 is assigned to data2.
When the result is displayed, the contents of data1 and data2 are the same.

Thus, with structure variables, all elements can be assigned at once.
As we will explain later, there are other ways to use the function, such as as function arguments, and so on.
A structure variable can be used as a variable in its own right, and
This is more convenient than arrays, which had to be assigned one by one.

Comparison of structure variables
We have explained that a structure variable can be used as a variable in its own right.
Unfortunately, operations and comparisons cannot be performed between structure variables. In other words, the

struct student data1,data2;
/* assign to data1 and data2 */
if (data1 == data2) {
/* some kind of processing */
}

You cannot write a program like
I don't care about addition or subtraction, but I think we should be able to do identical comparisons...


Until now, the structure tag was declared before the structure was used.
In this case, struct is required whenever a structure is used.
However, there is a way to declare the structure tag as a new type at once.

The C language provides a typedef to declare a new type.
We will explain in detail later, but the following typedef can be used to declare a new type.

Source Code
 typedef New type form New type name

This allows structure tags to be directly set to a new type.
The following program is an example of creating a new type based on a structure tag.

Source code
 struct student_tag
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
};

typedef struct student_tag student;

In this example, the student_tag tag can be of type student.
This way, when declaring structure variables, struct is no longer needed.
From now on, we will refer to types declared in the previous manner as structure types.

It is convenient to use a structure type because it can be treated like a type, but it is also convenient to use a
It is tedious to define a type using typedef every time.
To make things easier, you can declare a structure tag and a structure type at the same time.

Source Code
 typedef struct student_tag
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
} student;

Furthermore, the structure tag can be omitted in this case.

Source Code
 typedef struct
{
    int year; /* school year */
    int clas; /* class */
    int number; /* attendance number */
    char name[64]; /* name */
    double stature; /* height */
    double weight; /* weight */
} student;

This method is the most concise way to declare a structure type.
The above form is now almost idiomatic, so you can remember it in this form.


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 more complete 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 Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box