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


  Argument of structure   

  1. ..clause 1..: Pass information with the structure.
  2. ..clause 2..: The structure is a pointer variable.
  3. ..clause 3..: The structure is a pointer argument.

[1] Pass information with the structure.

As for the structure variable, it is treated as one variable.
Therefore, the argument of the structure type can be used, and two or more information can be passed to one degree.

The argument of the structure type of the current can be specified the argument and quite in a similar way.
However, it is necessary to apply struct before the name of tag, except for no declaration with typedef.
The following function is a function that receives the structure variable of the student type as an argument.

 
void student_print(student data)
The usage in the function on the received side is also quite the same as a usual argument.
The following function displays all content of the student type.
 
void student_print(student data) 
{
	"School year: %d\n ..printf (..", ,data.year);
	Printf ("[ Class ]: %d\n" data.clas);
	Printf ("[ Attendance number ]: %d\n" data.number);
	Printf ("[ Name ]: %s\n" data.name);
	Printf ("[ Height ]: %f\n" data.stature);
	Printf ("[ Weight ]: %f\n" data.weight);
	return; 
}
The argument at the array though it is only the initial address to be passed
As for the argument of the structure type, all the values are copied onto the function on the received side.
Therefore, even if contents of the argument are changed by the function on the received side, it doesn't influence former structure variable.

Usual can be called even on the call side the variable and quite in a similar way.
The following program is an example of calling the function ahead.

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

School year..class..attendance..number..name..height..weight. 
} student;  

void student_print(student data);  

int main(void)  
{
	student data;  

	data.year = 3;  
	data.clas = 4;  
	data.number = 18;  
	strcpy(data.name,"MARIO");  
	data.stature = 168.2;  
	data.weight = 72.4;  

	student_print(data); /* Call */

	return 0;  
}

void student_print(student data)  
{
	"School year: %d\n ..printf (..", ,data.year);  
	Printf ("[ Class ]: %d\n" data.clas);  
	Printf ("[ Attendance number ]: %d\n" data.number);  
	Printf ("[ Name ]: %s\n" data.name);  
	Printf ("[ Height ]: %f\n" data.stature);  
	Printf ("[ Weight ]: %f\n" data.weight);  
	return;
}
The execution result of this program becomes as follows.

[ School year ]: 3
[ Class ]: 4
[ Attendance number ]: 18
[ Name ]/MARIO
[ Height ]: 168.200000
[ Weight ]: 72.400000
This function displays all contents of the structure variable of the student type.

[ Array in   structure   ]
When the array is included in the structure, contents of the array are copied and passed.
Therefore, it calls even if contents are changed and it doesn't influence former variable.

It returns to contents.


[2] The structure is a pointer variable.

The pointer variable of the structure type can be made. The declaration method etc. are the same.
The following program is an example that uses the pointer variable to the student type.
 
#include <stdio.h> #include <string.h> 

School year..class..attendance..number..name..height..weight. 
} student;  

int main(void)  
{
	student data;  
	student *pdata;  
	
	Pdata =  &data;   /* initialization */
	(*pdata).year = 10; /* switch */ to variable mode usually Strcpy((*pdata).name,"MARIO"); /* Switch to variable mode usually */
	
	return 0;
}
Each element of the structure queues up in the declaration order.
The address requested by the operator is an address of the element of the start of the structure.

It is possible to usually switch to the variable mode by * sign for the pointer variable of the structure.
However, . Do as follows applying parentheses because it drinks and [kata] is given priority.


(* structure pointer variable name) . Element name
However, it can be substituted to apply (*) by the following writing because it is troublesome.

Structure pointer variable name-> element name
- > is a sign that combines the subtraction with the comparison sign.
* can be accessed to each element even if it doesn't mark it if it is this writing.
..the following program.. - > sign is used and example of the access to each element.
 int main(void) 
{
	student data; 
	student *pdata; 
	
	Pdata =  &data;   /* initialization */
	pdata->year = 10; /*- access */ by > sign Pdata ..strcpy (.. -);/*- Access by > sign */
	
	return 0; 
}
*This writing is easier than it marks and it encloses it with parentheses.

It returns to contents.


[3] The structure is a pointer argument.

Though it explained that it was possible to make it to the pointer variable even with the structure in the preceding clause
In the same way, the function with the argument of the pointer type to the structure type can be made.
The following program is an example of renewing to use the pointer variable the function ahead.

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

School year..class..attendance..number..name..height..weight. 
} student;  

void student_print(student *data);  

int main(void)  
{
	student data;  
	
	data.year = 3;  
	data.clas = 4;  
	data.number = 18;  
	strcpy(data.name,"MARIO");  
	data.stature = 168.2;  
	data.weight = 72.4;  
	
	student_print(&data); /* Call in the address */. 
	
	return 0;  
}

void student_print(student *data)  
{
	"School year: %d\n ..printf (..", ,data-{{>}}year); /*- Access it by > sign */ Printf ("[ The class ]: %d\n" data-> clas);  
	Printf ("[ Attendance number ]: %d\n" data-> number);  
	Printf ("[ Name ]: %s\n" data-> name);  
	Printf ("[ Height ]: %f\n" data-> stature);  
	Printf ("[ Weight ]: %f\n" data-> weight);  
	return;
}
First of all, it is understood that the type of the argument is declared as a pointer type.
It is understood to have passed the address applying the operator when the function is called.
Moreover, it accesses each element by > sign in the called function -.

Reason to pass structure that can be usually passed as pointer variable
The reason for eyes is that the value can be changed in the function as well as a usual pointer variable one.
It calls when the value is changed in the function and contents of former variable change though it doesn't try here, too.

The purpose secondarily is to speed up of the function call.
When the structure is passed, all the contents are copied.
It is wholly copied to the contents if in the structure, there is a big array.
This becomes naturally time-consuming as such processing.
However, only if the address value of the pointer is passed, it hardly takes time.

In general, though the structure is often handed over by using the pointer variable
Because it is easy to suffer from the trouble such as rewriting an inside value
I think that it is easier to hand it over usually of the experience.

It returns to contents.


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