快速提问如何为变量赋值数组元素的值。试图确保我的代码使用最小的内存。C变量赋值给数组元素
如果我有一个预定义的结构体数组,并且想要创建对数组中一个元素的引用,那么如何创建该变量以便通过引用传递而不是值?我搜索了这个,但也许我的搜索字符串不是它需要的。
例如
#myfile.h
typedef struct {
uint8_t abba;
uint8_t zabba;
} mystruct;
extern mystruct mystructs[2];
#myfile.c
mystruct mystructs[2] = {
{.abba=0,.zabba=1},
{.abba=2,.zabba=3}
};
void myfunc1() {
mystruct ms1 = mystructs[1];
printf("%d", ms1.abba);
ms1.zabba = 5;
}
void myfunc2() {
printf("%d", mystructs[1].abba);
mystructs[1].zabba = 5;
}
所以我的问题如下: 当我创建myfunc1 MS1,它只是给mystructs一个参考文献[1]?或者它将元素复制到ms1中?
myfunc1和myfunc2会在内存使用情况下产生相同的结果吗?
将ms1.zabba = 5实际更新mystructs [1] .zabba?
如果我能接受Peter Schneider和Quentin的答案。 Peter详细介绍了它的工作原理,Quentin给出了创建ms1作为指针的例子。 – user3817250