※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Bring the variable of a different type together.   

  1. ..clause 1..: When you want to treat data collectively
  2. ..clause 2..: How to use of the structure.
  3. ..clause 3..: Processing of structure variable
  4. ..clause 4..: Concise declaration of structure

[1] When you want to treat data collectively

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 */

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.
Because the height and weight are often expressed to the first decimal place, it makes it to the real type.

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.


[   Structure   ]
Type made bringing two or more different types together
In the structure, a new type that brings two or more types together can be produced.
For instance, do as follows to make student's data as a structure of a new type.
 School year..class..attendance..number..name..height..weight. 
};
Apply struct ([sutorakuto]) to the start when you declare the type of the structure.
The type name of the declared structure is given ..origin of the same rule as the variable.. newly as follows ..that...
This type name might be especially called a structure tag name.

[   Structure tag name   ]
Name of made structure deflecting.
Note that it is not a type strictly name.
This time, the name of student of the meaning called a student is given.
The name is declared to be a type of the variable to be brought together while ..[de].. having inclosed it now.

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.

 
struct student data;
In this example, the data structure variable of the student structure tag is declared.
Apply struct to the start when you declare the variable of the structure tag.
Next, specify the name of the structure variable at the type name of the structure and the end.
The variable to which the type of the structure is declared in the structure tag and the structure tag will be called a structure variable in the future.

[   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.
The structure tag and the structure variable can be declared by doing in this manner.
The following program is an example of declaring the structure tag and the structure variable.
 School year..class..attendance..number..name..height..weight. 
};

int main(void) 
{
	struct student data; 
	return 0; 
}
It is usual to declare the structure tag earlier than the function like this example.
Because this structure can be used if it does so by all functions that appear back.

It returns to contents.


[2] How to use of the structure.

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.
It can access an individual element by doing in this manner.
The following program is an example that uses the year element of the structure ahead.
 
#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; 
}
The execution result of this program becomes as follows.

10
In this example, the year element of the data structure variable of the student structure tag is used.
It is quite the same as an already usual variable when doing in this manner and accessing it.
Moreover, it can access the array in the same way.
The following program is an example of the access to the name element of the structure ahead.
 
#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;
}
The execution result of this program becomes as follows.

MARIO
Of course, it is also possible to access each element of the array applying [].

It returns to contents.


[3] Processing of structure variable

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.

 
#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 */ 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;
}
The execution result of this program becomes as follows.

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, first of all, it substitutes it for each element of data1.
And, data1 is substituted for data2.
When the result is displayed, contents of data1 and data2 are the same.

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.


[ 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
 
	struct student data1,data2;   Processing of something /*  */
	}
[Noyouna] [pu] log ram cannot be written.
After the structure tag had been declared up to now, the structure was used.
In this case, when the structure is used, struct is needed without fail.
However, there is a method of declaring the structure tag to one degree as a new type.

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.


Shape new [shii] type name with new  typedef type
Make the structure tag a personally new type when you use this.
The following program is an example of making a new type based on the structure tag.
 School year..class..attendance..number..name..height..weight. 
};

typedef struct student_tag student; 
In this example, the student_tag tag can be made student type.
When the structure variable is declared, struct becomes unnecessary if it does so.
The type declared in the method ahead will be called a structure type in the future.

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.

 School year..class..attendance..number..name..height..weight.                                    
} student;
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.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.