C language learned by suffering
C language learned by suffering
Arguments of the structure
Passing information by structure
A structure variable is treated as a single variable by itself.
Therefore, structure-type arguments can be used and multiple pieces of information can be passed at once.
Structure-type arguments can be specified in exactly the same way as previous arguments.
However, if the tag is not declared with typedef, struct must be prefixed to the tag name.
The following function takes a structure variable of type student as an argument.
The usage in the receiving function is exactly the same as for normal arguments.
The following function prints the entire contents of the STUDENT type.
With arrays, only the first address is passed as an argument, but with
structure type arguments, all values are copied to the receiving function.
Therefore, changing the contents of an argument in the receiving function does not affect the original structure variable.
It can be called by the caller in exactly the same way as an ordinary variable.
The following program is an example of calling the previous function.
The result of executing this program is as follows
This function prints the entire contents of a structure variable of type student.
Therefore, structure-type arguments can be used and multiple pieces of information can be passed at once.
Structure-type arguments can be specified in exactly the same way as previous arguments.
However, if the tag is not declared with typedef, struct must be prefixed to the tag name.
The following function takes a structure variable of type student as an argument.
Function that takes a structure variable of type student as an argument
void student_print(student data)
The usage in the receiving function is exactly the same as for normal arguments.
The following function prints the entire contents of the STUDENT type.
Function to display all content of type student
void student_print(student data)
{
printf("[grade]:%d\n", data.year);
printf("[class]:%d\n", data.class);
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;
}
With arrays, only the first address is passed as an argument, but with
structure type arguments, all values are copied to the receiving function.
Therefore, changing the contents of an argument in the receiving function does not affect the original structure variable.
It can be called by the caller in exactly the same way as an ordinary variable.
The following program is an example of calling the previous function.
Source Code
#include <stdio.h>
#include <string.h>
typedef struct
{
int year; /* school year */
int clas; /* class */
int number; /* attendance number */
char name[64]; /* name */
double stature; /* height */
double weight; /* weight */
} student;
void student_print(student data);
int main(void)
{
student data;
data.year = 3;
data.class = 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)
{
printf("[grade]:%d\n", data.year);
printf("[class]:%d\n", data.class);
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 result of executing this program is as follows
Execution Result
[Grade]:3
[Class]:4
[Attendance number: 18
[NAME]:MARIO
[Height: 168.200000
[Weight]:72.400000
[Class]:4
[Attendance number: 18
[NAME]:MARIO
[Height: 168.200000
[Weight]:72.400000
This function prints the entire contents of a structure variable of type student.
Array in structure
If the structure contains an array, the array contents are also copied and passed.
Thus, changing the contents does not affect the calling variable.
If you want to pass a copy of an array (for example, to pass board information in a reversi program), use the
structure is the easiest way to do this.
Thus, changing the contents does not affect the calling variable.
If you want to pass a copy of an array (for example, to pass board information in a reversi program), use the
structure is the easiest way to do this.
Pointer variable also in structure
It is possible to create pointer variables of structure type. The method of declaration is the same.
The following program is an example of using a pointer variable to a STUDENT type.
The elements of the structure are arranged in the order in which they were declared.
The address obtained by the & operator is the address of the beginning element of the structure.
In the case of pointer variables in structures, the * symbol can also be used to switch to normal variable mode.
However, . takes precedence over . so the following is used with parentheses.
However, it is tedious to add (*), so the following writing style can be used instead.
-> is a combination of the subtraction and comparison symbols.
As usual, it has nothing to do with subtraction or comparison, but only with the appropriation of symbols.
This writing style allows access to each element in a simpler format.
The following program is an example of using the -> symbol to access each element.
The following program is an example of using a pointer variable to a STUDENT type.
source code
#include <stdio.h>
#include <string.h>
typedef struct
{
int year; /* school year */
int clas; /* class */
int number; /* attendance number */
char name[64]; /* name */
double stature; /* height */
double weight; /* weight */
} student;
int main(void)
{
student data;
student *pdata;
pdata = &data; /* initialization */
(*pdata).year = 10; /* switch to normal variable mode */
strcpy((*pdata).name, "MARIO"); /* switch to normal variable mode */
return 0;
}
The elements of the structure are arranged in the order in which they were declared.
The address obtained by the & operator is the address of the beginning element of the structure.
In the case of pointer variables in structures, the * symbol can also be used to switch to normal variable mode.
However, . takes precedence over . so the following is used with parentheses.
Access elements via pointer variables in structure
(*structure pointer variable name). Element name
However, it is tedious to add (*), so the following writing style can be used instead.
Accessing elements via structure pointer variables
structure pointer variable name->element name
-> is a combination of the subtraction and comparison symbols.
As usual, it has nothing to do with subtraction or comparison, but only with the appropriation of symbols.
This writing style allows access to each element in a simpler format.
The following program is an example of using the -> symbol to access each element.
Source Code
int main(void)
{
student data;
student *pdata;
pdata = &data; /* initialization */
pdata->year = 10; /* ->access by symbol */
strcpy(pdata->name, "MARIO"); /* ->access by symbol */
return 0;
}
Pointer argument also in structure
In the previous section, we explained that a structure can also be a pointer variable.
In the same way, a function can be created with arguments of type pointer to a structure type.
The following program is an example of a function modified to use pointer variables.
First, we see that the argument types are declared as pointer types.
You can also see that when calling a function, the & operator is appended and the address is passed.
Also, within the called function, each element is accessed with the -> symbol.
The first is to allow the value to be changed within the function, just like an ordinary pointer variable.
Although not tested here, changing the value within a function also changes the contents of the caller's variable.
The second is to speed up function calls.
When passing a structure, all of its contents are copied.
If the structure contains a large array, the entire contents are copied.
This is naturally a reasonably time-consuming process.
However, if you just pass the pointer address value, it is very fast.
However, modern computers are so fast that even value passing is not much of a problem.
If you are unfamiliar with this method, we recommend that you use value-passing, as it is easier to handle.
In the same way, a function can be created with arguments of type pointer to a structure type.
The following program is an example of a function modified to use pointer variables.
source code
#include <stdio.h>
#include <string.h>
typedef struct
{
int year; /* school year */
int clas; /* class */
int number; /* attendance number */
char name[64]; /* name */
double stature; /* height */
double weight; /* weight */
} student;
void student_print(student *data);
int main(void)
{
student data;
data.year = 3;
data.class = 4;
data.number = 18;
strcpy(data.name, "MARIO");
data.stature = 168.2;
data.weight = 72.4;
student_print(&data); /* call at address */
return 0;
}
void student_print(student *data)
{
printf("[grade]:%d\n", data->year); /* access with -> symbol */
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;
}
First, we see that the argument types are declared as pointer types.
You can also see that when calling a function, the & operator is appended and the address is passed.
Also, within the called function, each element is accessed with the -> symbol.
The reason for passing a structure that can be passed normally as a pointer variable
The first is to allow the value to be changed within the function, just like an ordinary pointer variable.
Although not tested here, changing the value within a function also changes the contents of the caller's variable.
The second is to speed up function calls.
When passing a structure, all of its contents are copied.
If the structure contains a large array, the entire contents are copied.
This is naturally a reasonably time-consuming process.
However, if you just pass the pointer address value, it is very fast.
However, modern computers are so fast that even value passing is not much of a problem.
If you are unfamiliar with this method, we recommend that you use value-passing, as it is easier to handle.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.