- ..clause 1..: When you want to treat data collectively
- ..clause 2..: How to use of the structure.
- ..clause 3..: Processing of structure variable
- ..clause 4..: Concise declaration of structure
Chapter 13 explained the array treated bringing two or more variables of the same type together.
However, I occasionally want to treat a different type collectively according to circumstances.
Moreover, distinguishing by the name might be more convenient to distinguish each element by the number.
For instance, I want to preserve the result of student's body measurement at the school.
As for each student, the data of the class, the school year, the attendance number, the name, the height, and weight, etc. is necessary.
When this is obediently prepared by the variable, it becomes the following.
int year; /* school year */ Int clas;/* class */ Int number;/* attendance number */ Char name[64];/* name */ Double stature;/* height */ Double weight;/* Weight */
Because the height and weight are often expressed to the first decimal place, it makes it to the real type.
Clas Though class is a correct spelling
In C++ Because class dares to be a reserved word It makes it to clas.
If you do not like it Group might be acceptable.
Naturally, this functions enough as student's data.
However, though these data is all the relating data
Because each one is declared as another variable, it is not so comprehensible.
As a method of handling collectively in such a case in one of the variable of two or more different types
The function of structure is prepared.
In the structure, a new type that brings two or more types together can be produced.
[ Structure ]
Type made bringing two or more different types together
Apply struct ([sutorakuto]) to the start when you declare the type of the structure.School year..class..attendance..number..name..height..weight. };
This time, the name of student of the meaning called a student is given.
[ Structure tag name ]
Name of made structure deflecting.
Note that it is not a type strictly name.
Though the type of the novel structure can be made by doing in this manner
It is to merely declare the type, it is not possible to actually use it being possible this to hold.
It is necessary to declare the variable of the type of the structure to actually use it.
Do as follows to declare the variable of the type of the structure.
In this example, the data structure variable of the student structure tag is declared.struct student data;
The structure tag and the structure variable can be declared by doing in this manner.
[ C++ ]
In C++ that is the extended version of C language
Even if struct is not applied, the structure variable can be declared.
Most : a present compiler because it is for C++.
Even if struct is not applied, it is possible to declare.
It is usual to declare the structure tag earlier than the function like this example.School year..class..attendance..number..name..height..weight. }; int main(void) { struct student data; return 0; }
In the preceding clause, the declaration of the structure tag and the structure variable though it explained
It is not significant to no use actually no matter how it declares.
The structure variable has all types declared in the structure tag that became an origin.
When arranging it though the variable of the same type was distinguished according to the number
In the structure variable, distinguish all elements according to the name regardless of the type.
Do as follows to access one element of the structure variable.
Here, . is a decimal marker. It is not a comma.構造体変数名.要素名
The execution result of this program becomes as follows.#include <stdio.h> School year..class..attendance..number..name..height..weight. }; int main(void) { struct student data; data.year = 10; /* It accesses the year element * / Printf("%d\n",data.year); return 0; }
In this example, the year element of the data structure variable of the student structure tag is used.
10
The execution result of this program becomes as follows.#include <stdio.h> #include <string.h> School year..class..attendance..number..name..height..weight. }; int main(void) { struct student data; strcpy(data.name,"MARIO"); printf("%s\n",data.name); return 0; }
Of course, it is also possible to access each element of the array applying [].
MARIO
Though it explained the method of the access to each element of the structure in the preceding clause
With this, though it seems to be certainly settled to externals
An actual usage seems not to be so significant quite the same as a usual variable.
However, the structure variable can be handled for the structure as a variable.
For instance, it becomes possible to substitute in the structure variable.
The following program is an example for substitution in the structure variable.
The execution result of this program becomes as follows.#include <stdio.h> #include <string.h> School year..class..attendance..number..name..height..weight. }; int main(void) { struct student data1,data2; /* Substitute it for data1 */ FONT> data1.year = 3; data1.clas = 4; data1.number = 18; strcpy(data1.name,"MARIO"); data1.stature = 168.2; data1.weight = 72.4; data2 = data1; /* Copy the content of data1 to data2 */. /* The content of data1 and data2 is displayed */ 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; }
In this program, first of all, it substitutes it for each element of data1.
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
All elements can be substituted in the structure variable in this manner in bulk.
Though it explains later, it uses it as other arguments of the function.
The structure variable can use it as one variable.
It is more convenient than the array that had to be substituted one by one.
After the structure tag had been declared up to now, the structure was used.
[ Comparison of structure variables ]
Though the structure variable explained that it can be used as one variable
To our regret, the operation and the comparison by the structure variable cannot be done. In a word
[Noyouna] [pu] log ram cannot be written.struct student data1,data2; Processing of something /* */ }
In C language, typedef ([taipudefu]) to declare a new type is prepared.
A new type can be declared by using typedef as follows though a detailed explanation will be done later.
Make the structure tag a personally new type when you use this.Shape new [shii] type name with new typedef FONT> type
In this example, the student_tag tag can be made student type.School year..class..attendance..number..name..height..weight. }; typedef struct student_tag student;
However, it is therefore troublesome to define the type by using typedef.
In that case, the structure tag and the structure type can be declared to one degree.
If a new type can be defined in addition in this case, it is ..structure tag.. omissible.School year..class..attendance..number..name..height..weight. } student;
This first concise method can declare the structure type.School year..class..attendance..number..name..height..weight. } student;