MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

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.

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("[学year]:%d\n", data.year);
    printf("[クラス]:%d\n", data.clas);
    printf("[出席番号]:%d\n", data.number);
    printf("[名前]:%s\n", data.name);
    printf("[身長]:%f\n", data.stature);
    printf("[体重]:%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;       /* 学year */
    int clas;       /* クラス */
    int number;     /* 出席番号 */
    char name[64];  /* 名前 */
    double stature; /* 身長 */
    double 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); /* 呼び出し */

    return 0;
}

void student_print(student data)
{
    printf("[学year]:%d\n", data.year);
    printf("[クラス]:%d\n", data.clas);
    printf("[出席番号]:%d\n", data.number);
    printf("[名前]:%s\n", data.name);
    printf("[身長]:%f\n", data.stature);
    printf("[体重]:%f\n", data.weight);
    return;
}

The output of this program is as follows:

Execution results
[学year]:3
[クラス]:4
[出席番号]:18
[名前]:MARIO
[身長]:168.200000
[体重]:72.400000

This function displays the contents of a student type structure variable.

Arrays within structures
structの中にarrayが含まれている場合は、arrayの中身もcopyされて渡されます。
Therefore, modifying the contents will not affect the caller's variable.
arrayをcopyして渡したいHour(たとえば、リバーシのプログラムで盤面情報を渡したい等)には、
It's easiest to make it a struct.

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.

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

typedef struct
{
    int year;       /* 学year */
    int clas;       /* クラス */
    int number;     /* 出席番号 */
    char name[64];  /* 名前 */
    double stature; /* 身長 */
    double weight;  /* 体重 */
} student;

int main(void)
{
    student data;
    student *pdata;

    pdata = &data;                  /* Initialization */
    (*pdata).year = 10;             /* normalvariableモードへの切り替え */
    strcpy((*pdata).name, "MARIO"); /* normalvariableモードへの切り替え */

    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 structure.
(*structPointer variables名).要素名

"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 structure.
structPointer variables名->要素名

"-> is a symbol that combines subtraction and comparison operators."
例によって、引き算とも比較ともまったく関係はなく、symbolを流用しているだけです
"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;             /* ->symbolによるアクセス */
    strcpy(pdata->name, "MARIO"); /* ->symbolによるアクセス */

    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.

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

typedef struct
{
    int year;       /* 学year */
    int clas;       /* クラス */
    int number;     /* 出席番号 */
    char name[64];  /* 名前 */
    double stature; /* 身長 */
    double 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); /* addressで呼び出す */

    return 0;
}

void student_print(student *data)
{
    printf("[学year]:%d\n", data->year); /* ->symbolでアクセス */
    printf("[クラス]:%d\n", data->clas);
    printf("[出席番号]:%d\n", data->number);
    printf("[名前]:%s\n", data->name);
    printf("[身長]:%f\n", data->stature);
    printf("[体重]:%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) is
This 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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19