这段代码是否正确?它可以按预期运行,但是这段代码是否正确地使用了指针和结构体的点符号表示法?
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);
}
someStruct
具有值语义,在C++术语中,“按引用传递”(在C中不这样做)与“通过值传递引用”完全相同(它确实这样做,所谓的“引用”是指针)。 - Steve Jessop