For example, if you want to save students' physical measurement results at school,
For each student, we need data such as class, grade level, attendance number, name, height, and weight.
"If we simply prepare this as a variable, it would look like this:"
Source code
int year; /* 学year */
int clas; /* クラス */
int number; /* 出席番号 */
char name[64]; /* 名前 */
double stature; /* 身長 */
double weight; /* 体重 */
clas?
"clas is class spelled correctly." However, since the name "class" is not available, I'm using "clas" instead.
なぜなら、ほとんどのC languageCompilerはC++にも対応していますが、 C++ has a feature called classes, and "class" is used to define them. In C++, "class" is a reserved word, so it cannot be used as a name. If you don't like clas, we can do group instead.
"Height and weight are often expressed to one decimal place, so they are stored as floating-point numbers."
Of course, this will still function well enough as student data.
However, despite all of this data being relevant,
It's not very clear because each one is declared as a separate variable.
As a way to handle multiple variables of different types together,
A structure function is provided.
キーワード
【struct】
A type created by combining multiple distinct types.
Structures allow you to create new types by combining multiple types.
For example, to create student data as a new type of struct, you would do it like this.
When declaring a structure type, you begin with struct.
Next, we'll name the newly declared struct type using the same rules as variables.
This type may be referred to as the structure tag name in particular.
キーワード
【Struct tag name】
the name of the structure itself. Because it's not a type name, it's handled a bit differently.
This time, we're using the name "student," which means student.
“Then, you declare the type and name of the variables you want to summarize within the curly braces { }.”
In this way, new structure types can be created.
This alone is just a type declaration, so it can't be used directly.
You need to declare a variable of a struct type to use it.
To declare a variable of a struct type, do the following.
Declare a variable of a struct type.
struct student data;
In this example, we are declaring a data structure variable with the student structure tag.
When declaring variables for structure tags, you must begin with `struct`.
Next, specify the structure type name, and finally, the name of the structure variable.
Going forward, we will refer to the type of a structure as a structure tag and variables declared with a structure tag as structure variables.
In C++
C languageの拡張版であるIn C++ You can declare structure variables without using the `struct` keyword. 現在のCompilerはほとんどがC++用なので、 Often, it works even without using `struct`.
"This is how you can declare structure tags and structure variables."
The following program demonstrates how to declare a structure tag and a structure variable.
As in this example, struct tags are typically declared before the function.
Because then this structure can be used in all subsequent functions.
How to Use Structures
The previous section explained struct tags and struct variable declarations.
Let's start using it, now that we've declared the struct!
Structure variables have all the types that were declared in the struct tag they were based on.
When dealing with arrays, we distinguished variables of the same type by number.
In structure variables, all elements are distinguished by name, regardless of type.
To access one element of a structure variable, you do it like this:
Accessing a single element of a structure variable.
structvariable名.要素名
Here, . represents the decimal point.It's not a comma.
However, it simply reuses the decimal point symbol and has nothing to do with the decimal fraction itself.
Pointerといい、C languageって無関係のsymbolを流用する悪い癖があるよね・・・
In this way, you can access individual elements.
The following program demonstrates an example using the year element of the structure mentioned previously.
In this example, we are using the year element of the data structure variable with the student structure tag.
Accessed in this way, it is now indistinguishable from a regular variable.
Similarly, you can also access elements in an array.
The following program demonstrates accessing the name element of the structure from earlier.
Of course, you can also access each element of the array using [].
Handling struct variables themselves
In the previous section, we described how to access each element of a structure.
While it may appear cohesive at first glance,
In practice, it's exactly the same as a regular variable, and it might even seem pointless.
However, with structs, you can treat the struct variable itself as a variable.
For example, it is possible to assign one struct variable to another.
The following program demonstrates assignment between struct variables.
This program first assigns a value to each element of data1.
And I am assigning data1 to data2.
When we display the results, the contents of data1 and data2 are the same.
"In this way, you can assign all elements of a structure variable at once."
As I will explain later, there are other uses, such as utilizing them as function arguments, and so on.
Structure variables can be used as a single variable themselves.
It's more convenient than having to assign each element individually.
Comparing Struct Variables
structvariableは、それ自体を1つのvariableとして使えるとDescriptionしましたが、 Unfortunately, operations and comparisons between struct variables are not possible.In other words,
Previously, we were declaring struct tags before using the struct.
In this case, the keyword `struct` is always required when using structures.
However, there is a way to declare struct tags as a new type all at once.
C offers a feature called `typedef` to declare new types.
I'll provide more details later, but you can declare a new type using typedef as follows.
Source code
typedef 新しい型の形 新しい型名
"This allows you to directly make struct tags into new types."
"The following program demonstrates an example of creating a new type based on a struct tag."
In this example, you can make the student_tag tag a student type.
"This way, you don't need the `struct` keyword when declaring structure variables."
From now on, we will refer to types declared in the manner described previously as struct types.
"Making it a structure type allows it to be treated like a type, which is convenient."
It's tedious to define types using typedef every time.
For simplicity, you can declare struct tags and struct types simultaneously.
This is the most concise way to declare a structure type.
"The form above has essentially become a conventional phrase, so feel free to memorize it as is."
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.