Structure arguments
Passing information via structs
Structure variables are treated as a single variable in themselves.
Therefore, we can use struct-type arguments, allowing us to pass multiple pieces of information at once.
Struct-like arguments can be specified in exactly the same way as previous arguments.
However, you must prefix the struct with a tag name if you haven't declared it with typedef.
The following function takes a student-type structure variable as an argument.
It's used in the receiving function exactly like a regular argument.
The following function displays all the contents of a student type.
When dealing with arrays, whether as arguments or passed as values, only the address of the first element was passed.
Struct arguments are passed by value to the receiving function.
Therefore, modifying the contents of an argument within a receiving function will not affect the original struct variable.
It can be called just like a regular variable.
The following program is an example of calling the function mentioned earlier.
The output of this program is as follows.
This function displays the contents of a student type structure variable.
Therefore, we can use struct-type arguments, allowing us to pass multiple pieces of information at once.
Struct-like arguments can be specified in exactly the same way as previous arguments.
However, you must prefix the struct with a tag name if you haven't declared it with typedef.
The following function takes a student-type structure variable as an argument.
A function that accepts a struct variable of type student as an argument.
void student_print(student data)
It's used in the receiving function exactly like a regular argument.
The following function displays all the contents of a student type.
A function that displays all contents of a Student type.
void student_print(student data)
{
printf("[Grade]:%d\n", data.year);
printf("[Class]:%d\n", data.clas);
printf("[Seat number]:%d\n", data.number);
printf("[name]:%s\n", data.name);
printf("[Height]:%f\n", data.stature);
printf("[Weight]:%f\n", data.weight);
return;
}
When dealing with arrays, whether as arguments or passed as values, only the address of the first element was passed.
Struct arguments are passed by value to the receiving function.
Therefore, modifying the contents of an argument within a receiving function will not affect the original struct variable.
It can be called just like a regular variable.
The following program is an example of calling the function mentioned earlier.
Source code
#include <stdio.h>
#include <string.h>
typedef struct
{
int year; /* Grade */
int clas; /* Class */
int number; /* Seat 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.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)
{
printf("[Grade]:%d\n", data.year);
printf("[Class]:%d\n", data.clas);
printf("[Seat 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 output of this program is as follows.
Results
[Grade]:3
[Class]:4
[Seat number]:18
[name]:MARIO
[Height]:168.200000
[Weight]:72.400000
[Class]:4
[Seat number]:18
[name]:MARIO
[Height]:168.200000
[Weight]:72.400000
This function displays the contents of a student type structure variable.
Arrays within structures
If a structure contains an array, the contents of the array are also copied and passed. Therefore, modifying its contents does not affect the variable in the calling function. When you want to pass an array by copying it (for example, to pass board information in a Reversi program), the simplest approach is to wrap it in a structure.
Structures can be pointer variables too.
You can create a pointer variable of a structure type.The methods of declaration are the same.
The following program demonstrates the use of a pointer variable to a student type.
The elements of a structure are arranged in the order they are declared.
The address obtained with the & operator is the address of the first element of the structure.
You can also switch to normal variable mode using the * symbol when dealing with pointer variables of structures.
However, since the dot has higher precedence, we enclose it in parentheses like this.
However, to avoid the trouble of adding (*), it's been set up to allow for the following alternative.
-> is a symbol that combines subtraction and comparison operators.
As usual, it has absolutely nothing to do with subtraction or comparison; it's just reusing the symbols.
This way, you can access each element with a simpler format.
The following program demonstrates accessing each element using the -> symbol.
The following program demonstrates the use of a pointer variable to a student type.
Source code
#include <stdio.h>
#include <string.h>
typedef struct
{
int year; /* Grade */
int clas; /* Class */
int number; /* Seat 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; /* Switching to Normal Variable Mode */
strcpy((*pdata).name, "MARIO"); /* Switching to Normal Variable Mode */
return 0;
}
The elements of a structure are arranged in the order they are declared.
The address obtained with the & operator is the address of the first element of the structure.
You can also switch to normal variable mode using the * symbol when dealing with pointer variables of structures.
However, since the dot has higher precedence, we enclose it in parentheses like this.
Accessing elements via a pointer to a struct.
(*Structure pointer variable name).Element name
However, to avoid the trouble of adding (*), it's been set up to allow for the following alternative.
Accessing elements via a pointer to a struct.
Structure pointer variable name->Element name
-> is a symbol that combines subtraction and comparison operators.
As usual, it has absolutely nothing to do with subtraction or comparison; it's just reusing the symbols.
This way, you can access each element with a simpler format.
The following program demonstrates accessing each element using the -> symbol.
Source code
int main(void)
{
student data;
student *pdata;
pdata = &data; /* Initialization */
pdata->year = 10; /* ->Symbolic access */
strcpy(pdata->name, "MARIO"); /* ->Symbolic access */
return 0;
}
Structure arguments as well.
As we explained in the previous section, structures can also be pointer variables.
Similarly, functions accepting pointer types as arguments to struct types can also be created.
The following program is an example of modifying the previous function to use pointer variables.
First, we can see that the argument is declared as a pointer type.
It's also evident that when calling the function, you're passing the address by using the & operator.
Also, within the called function, we are accessing each element using the -> symbol.
Reasons for passing a struct that could be passed by value as a pointer variable.
The first is to allow the value to be changed within the function, just like with a regular pointer variable.
I haven't tried it here, but modifying a value within a function changes the content of the variable in the calling scope.
Secondly, it's for speeding up function calls.
When passing a structure, its contents are all copied.
If a structure contains a large array, the entire array will be copied as is.
This will naturally be a time-consuming process.
However, passing only the address of a pointer is very fast.
However, modern computers are so fast that pass-by-value is not a significant issue.
While you're still getting used to it, market orders are generally easier to manage, so we recommend using them.
Similarly, functions accepting pointer types as arguments to struct types can also be created.
The following program is an example of modifying the previous function to use pointer variables.
Source code
#include <stdio.h>
#include <string.h>
typedef struct
{
int year; /* Grade */
int clas; /* Class */
int number; /* Seat 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.clas = 4;
data.number = 18;
strcpy(data.name, "MARIO");
data.stature = 168.2;
data.weight = 72.4;
student_print(&data); /* Call by address */
return 0;
}
void student_print(student *data)
{
printf("[Grade]:%d\n", data->year); /* ->Access via symbol */
printf("[Class]:%d\n", data->clas);
printf("[Seat 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 can see that the argument is declared as a pointer type.
It's also evident that when calling the function, you're passing the address by using the & operator.
Also, within the called function, we are accessing each element using the -> symbol.
Reasons for passing a struct that could be passed by value as a pointer variable.
The first is to allow the value to be changed within the function, just like with a regular pointer variable.
I haven't tried it here, but modifying a value within a function changes the content of the variable in the calling scope.
Secondly, it's for speeding up function calls.
When passing a structure, its contents are all copied.
If a structure contains a large array, the entire array will be copied as is.
This will naturally be a time-consuming process.
However, passing only the address of a pointer is very fast.
However, modern computers are so fast that pass-by-value is not a significant issue.
While you're still getting used to it, market orders are generally easier to manage, so we recommend using them.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




