- ..clause 1..: Pass information with the structure.
- ..clause 2..: The structure is a pointer variable.
- ..clause 3..: The structure is a pointer argument.
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.
The usage in the function on the received side is also quite the same as a usual argument.void student_print(student data)
The argument at the array though it is only the initial address to be passedvoid 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; }
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.
The execution result of this program becomes as follows.#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; }
This function displays all contents of the structure variable of the student type.
[ School year ]: 3
[ Class ]: 4
[ Attendance number ]: 18
[ Name ]/MARIO
[ Height ]: 168.200000
[ Weight ]: 72.400000
[ 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.
The pointer variable of the structure type can be made. The declaration method etc. are the same.
Each element of the structure queues up in the declaration order.#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; }
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.
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.
Structure pointer variable name-> element name
*This writing is easier than it marks and it encloses it with parentheses.int main(void) { student data; student *pdata; Pdata = &data; /* initialization */ pdata->year = 10; /*- access */ by > sign Pdata ..strcpy (.. -);/*- Access by > sign */ return 0; }
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.
First of all, it is understood that the type of the argument is declared as a pointer type.#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 */. FONT> 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; }
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.