[英]Pass struct by reference in C
這段代碼正確嗎? 它按預期運行,但是這段代碼是否正確使用了結構的指針和點表示法?
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;
將創建兩個指針p1
和p2
以及一個變量var1
。 編寫struct someStruct* p1, p2, var1;
將創建單個指針p1
和兩個變量p2
和var1
// 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;
}
這是結構的正確用法。 有關於您的返回值的問題。
此外,因為您正在打印一個 unsigned int,所以您應該使用%u
而不是%d
。
恩,那就對了。 它創建一個 struct s
,將其總數設置為 5,將指向它的指針傳遞給使用該指針將總數設置為 4 的函數,然后將其打印出來。 ->
用於指向結構體和.
用於結構成員。 就像你使用它們一樣。
但是返回值是不同的。 test
可能應該是無效的,並且main
在其末尾需要return 0
。
是的。 這是正確的。 如果不是(從 . / -> 的角度來看),您的編譯器會大喊大叫。
是的,它正確使用了結構。 你也可以使用
typedef struct someStruct {
unsigned int total;
} someStruct;
那么你就不必寫struct someStruct s;
一次又一次但可以使用someStruct s;
然后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.