gpt4 book ai didi

c - 在 C 中通过引用传递结构

转载 作者:太空狗 更新时间:2023-10-29 16:22:58 24 4
gpt4 key购买 nike

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

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);
}

最佳答案

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

这是您的代码的副本,其中包含一些额外的注释以及在使用结构和指针以及函数和变量范围方面需要考虑的事项。

注意:下面源代码示例中的代码编写差异是我在结构名称之后和函数定义/声明中的星号之前放置了一个空格,如 struct someStruct *p1; 并且 OP 在星号后放置一个空格,如 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;
}

/*
* Make a local copy of the argument then modify the local copy.
* Until the assignment of the local copy to the argument is made,
* the changes to the local copy are not made to the argument.
* To make any changes made to the local copy in the argument,
* you need to assign the local copy to the argument.
*/
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;
}

关于c - 在 C 中通过引用传递结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4316314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com