简体   繁体   English

将结构发送到C中的函数

[英]Sending structures to functions in C

I have a sample piece of code and I had a question about it. 我有一段示例代码,对此有一个疑问。 Here is the code. 这是代码。 My questions are comments near the bottom of the code and I was wondering if anyone could help out. 我的问题是代码底部的注释,我想知道是否有人可以提供帮助。 Getting mixed up here. 在这里变得混乱。

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

struct car {
    char make[30];
    char model[30];
    int year;
};

struct car f(struct car c);

int main(void)
{
    struct car c1 = {"Ford", "Mustang", 2014}, c2;
    c2 = c1;

    printf("%s, %s, %d\n", c1.make, c1.model, c1.year);
    c2 = f(c1);
    printf("%s, %s, %d\n", c2.make, c2.model, c2.year);
}

struct car f(struct car c) /* Is car here referring to the structure at the very top
                             of the program, so it knows what structure to put it? */
{
    struct car temp = c;  /* What exactly is c in this function and why copy it? */
    strcpy(temp.model, "Explorer");
    temp.year += 5;

    return temp;
}

The code prints: 代码显示:

Ford, Mustang, 2014
Ford, Explorer, 2019

struct car is a type, which is defined at the top of your program. struct car是一种类型,它在程序的顶部定义。 So struct car f(struct car c) is a function named f that takes a struct car named c as a parameter, and returns another struct car . 因此struct car f(struct car c)是一个名为f的函数,该函数将名为cstruct car作为参数,并返回另一个struct car

The reason to make a copy of c is so that you can change one of them without affecting the other. 复制c的原因是,您可以更改其中一个而不影响另一个。 Since you passed c by value in this case, it doesn't really matter; 由于在这种情况下您按值传递了c ,所以这并不重要。 you could edit c and then return it. 您可以编辑c ,然后将其返回。 But if you'd passed it by reference (ie, passing a pointer to it), then modifying it would change the caller's copy, which might not be what you want. 但是,如果您通过引用传递了它(即传递了指向它的指针),则对其进行修改将更改调用者的副本,而这可能并不是您想要的。

When you call f() the struct is copied and then passed in to the function. 调用f() ,将复制该结构,然后将其传递给该函数。 Any modifications made in the function will be discarded upon exit. 对函数所做的任何修改将在退出时被丢弃。 However, you are passing out a struct which you then save and print. 但是,您正在传递一个结构,然后保存并打印。 So, to answer your question, the struct car in the function is a temporary variable that does not directly refer back to anything external to the function. 因此,为回答您的问题,该struct car中的struct car是一个临时变量,它不会直接引用该函数外部的任何内容。

If you want to directly modify a variable outside the function, then you will need to pass a pointer instead and dereference that pointer inside the function. 如果要直接在函数外部修改变量,则需要传递一个指针,然后在函数内部取消引用该指针。

struct car f(struct car c) /*is car here refering to the structure at the very top
                            of the program, so it knows what structure to put it?/*

struct car here is a type 'struct car' defining X . struct car在这里是一个type 'struct car' defining X The struct car at the top defines the global type struct car . 顶部的struct car定义了全局类型的struct car X refers to the function f that provides a return of type struct car and c an argument that expects a reference of type struct car (regardless what name it has). X指的是函数f ,它提供了struct car类型的返回值,而c是一个参数,期望得到struct car类型的引用(无论它具有什么名称)。 The struct car at the top has global scope, the function above expect an agrument such as c1 or c2 declared in main(). 顶部的struct car具有全局作用域,以上函数期望在main()中声明诸如c1c2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM