Easy Tutorial
❮ Shortcut Key Android Tutorial Colorfilterl2 ❯

English: ## C Language Passing Structures as Function Parameters

Category Programming Techniques

There are three ways to pass a structure as a function parameter:

Firstly, passing a structure variable, which is pass-by-value. Secondly, passing a pointer to a structure, which is pass-by-address. Thirdly, passing a member of a structure, which can also be either pass-by-value or pass-by-address.

Passing a structure by reference is more efficient than passing by value. Passing by value requires copying the entire structure.

Let's look at an example where the student structure contains various information about a student. We make some modifications to it in the change function and then output the result in the main function.

1. Passing a Structure Variable

#include<stdio.h>
#include<string.h>
#define format "%d\n%s\n%f\n%f\n%f\n"
struct student
{
    int num;
    char name[20];
    float score[3];
};
void change(struct student stu);
int main()
{
    struct student stu;
    stu.num = 12345;
    strcpy(stu.name, "Tom");
    stu.score[0] = 67.5;
    stu.score[1] = 89;
    stu.score[2] = 78.6;
    change(stu);
    printf(format, stu.num, stu.name, stu.score[0], stu.score[1], stu.score[2]);
    printf("\n");
    return 0;
}

void change(struct student stu)
{
    stu.score[0] = 100;
    strcpy(stu.name, "jerry");
}

It can be seen that the final output values have not changed...

2. Address Passing

#include<stdio.h>
#define format "%d\n%s\n%f\n%f\n%f\n"
struct student
{
    int num;
    char name[20];
    float score[3];
};
void change(struct student* stu);
int main()
{
    struct student stu;
    stu.num = 12345;
    strcpy(stu.name, "Tom");
    stu.score[0] = 67.5;
    stu.score[1] = 89;
    stu.score[2] = 78.6;
    change(&stu);
    printf(format, stu.num, stu.name, stu.score[0], stu.score[1], stu.score[2]);
    printf("\n");
    return 0;
}

void change(struct student* p)
{
    p->score[0] = 100;
    strcpy(p->name, "jerry");
}

It can be seen that the data inside the structure has been modified through address passing.

Using &stu as the actual parameter, &stu is the address of the structure variable stu. When calling the function, this address is passed to the formal parameter p (p is a pointer variable). In this way, p points to stu.

In the change function, the value of the member inside the structure is changed, and the changed value is output in the main function.

3. Address Passing and Value Passing of Structure Members

This is similar to the passing of a single variable, and there is no need to elaborate here. Of course, only address passing can modify it.

Passing a complete structure variable as a parameter requires passing all member values one by one, which is time-consuming and space-consuming, and the cost is high. If there are many members in the structure type, or some members are arrays, the program's execution efficiency will be greatly reduced. In this case, using a pointer as a function parameter is better, which can improve the efficiency of the program.

>

Original article link: https://blog.csdn.net/lin37985/article/details/38582027

** Click to Share Notes

Cancel

-

-

-

❮ Shortcut Key Android Tutorial Colorfilterl2 ❯