C语言 在 C 中通过引用传递结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4316314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:15:02  来源:igfitidea点击:

Pass struct by reference in C

cstruct

提问by Donald

Is this code correct? It runs as expected, but is this code correctly using the pointers and dot notation for the struct?

这段代码正确吗?它按预期运行,但是这段代码是否正确使用了结构的指针和点表示法?

struct someStruct {
 unsigned int total;
};

int test(struct someStruct* state) {
 state->total = 4;
}

int main () {
 struct someStruct s;
 s.total = 5;
 test(&s);
 printf("\ns.total = %d\n", s.total);
}

回答by Richard Chambers

Your use of pointer and dot notation is good. The compiler should give you errors and/or warnings if there was a problem.

您对指针和点符号的使用很好。如果有问题,编译器应该给你错误和/或警告。

Here is a copy of your code with some additional notes and things to think about so far as the use of structs and pointers and functions and scope of variables.

这是您的代码的副本,其中包含一些额外的注意事项,以及就结构体、指针和函数的使用以及变量的作用域而言需要考虑的事项。

Note:A code writing difference in the source example below is I put a space after the struct name and before the asterisk in the function definition/declaration as in struct someStruct *p1;and the OP put a space after the asterisk as in struct someStruct* p1;. There is no difference to the compiler, just a readability and habit difference for the programmer. I prefer putting the asterisk next to the variable name to make clear the asterisk changes the variable name it is next to. This is especially important if I have more than one variable in a declaration or definition. Writing struct someStruct *p1, *p2, var1;will create two pointers, p1and p2, and a variable, var1. Writing struct someStruct* p1, p2, var1;will create single pointer, p1and two variables p2and var1

注意:下面源代码示例中的代码编写差异是,我在函数定义/声明中的结构名称之后和星号之前struct someStruct *p1;放置了一个空格,如 in ,而 OP 在星号之后放置了一个空格,如 in struct someStruct* p1;。编译器没有区别,只是程序员的可读性和习惯不同。我更喜欢将星号放在变量名旁边,以明确星号会更改它旁边的变量名。如果声明或定义中有多个变量,这一点尤其重要。写入struct someStruct *p1, *p2, var1;将创建两个指针p1p2,以及一个变量,var1。写入struct someStruct* p1, p2, var1;将创建单个指针,p1以及两个变量p2var1

// Define the new variable type which is a struct.
// This definition must be visible to any function that is accessing the
// members of a variable of this type.
struct someStruct {
    unsigned int total;
};

/*
 * Modifies the struct that exists in the calling function.
 *   Function test() takes a pointer to a struct someStruct variable
 *   so that any modifications to the variable made in the function test()
 *   will be to the variable pointed to.
 *   A pointer contains the address of a variable and is not the variable iteself.
 *   This allows the function test() to modify the variable provided by the
 *   caller of test() rather than a local copy.
 */
int test(struct someStruct *state) {
    state->total = 4;
    return 0;
}

/* 
 * Modifies the local copy of the struct, the original
 * in the calling function is not modified.
 * The C compiler will make a copy of the variable provided by the
 * caller of function test2() and so any changes that test2() makes
 * to the argument will be discarded since test2() is working with a
 * copy of the caller's variable and not the actual variable.
 */
int test2(struct someStruct state) {
    state.total = 8;
    return 0;
}

int test3(struct someStruct *state) {
    struct someStruct  stateCopy;
    stateCopy = *state;    // make a local copy of the struct
    stateCopy.total = 12;  // modify the local copy of the struct
    *state = stateCopy;    /* assign the local copy back to the original in the
                              calling function. Assigning by dereferencing pointer. */
    return 0;
}

int main () {
    struct someStruct s;

    /* Set the value then call a function that will change the value. */
    s.total = 5;
    test(&s);
    printf("after test(): s.total = %d\n", s.total);

    /*
     * Set the value then call a function that will change its local copy 
     * but not this one.
     */
    s.total = 5;
    test2(s);
    printf("after test2(): s.total = %d\n", s.total);

    /* 
     * Call a function that will make a copy, change the copy,
       then put the copy into this one.
     */
    test3(&s);
    printf("after test3(): s.total = %d\n", s.total);

    return 0;
}

回答by Bill Lynch

That's correct usage of the struct. There are questions about your return values.

这是结构的正确用法。有关于您的返回值的问题。

Also, because you are printfing a unsigned int, you should use %uinstead of %d.

此外,因为你是一个printfing unsigned int类型,你应该使用%u代替%d

回答by nmichaels

Yes, that's right. It makes a struct s, sets its total to 5, passes a pointer to it to a function that uses the pointer to set the total to 4, then prints it out. ->is for members of pointers to structs and .is for members of structs. Just like you used them.

恩,那就对了。它创建一个 struct s,将其总数设置为 5,将指向它的指针传递给使用该指针将总数设置为 4 的函数,然后将其打印出来。->用于指向结构的指针的成员和.用于结构的成员。就像你使用它们一样。

The return values are different though. testshould probably be void, and mainneeds a return 0at its end.

但是返回值是不同的。test应该可能是无效的,并且main需要return 0在其末尾。

回答by Raveline

Yep. It's correct. If it wasn't (from the . / -> point of view), your compiler would yell.

是的。这是正确的。如果不是(从 . / -> 的角度来看),您的编译器会大喊大叫。

回答by Waqar Rashid

Yes, its correct usage of structures. You can also use

是的,它正确使用了结构。你也可以使用

typedef struct someStruct {
 unsigned int total;
} someStruct;

Then you won't have to write struct someStruct s;again and again but can use someStruct s;then.

然后你就不必struct someStruct s;一次又一次地写 ,但可以使用someStruct s;