我仍在学习指针和结构,但我希望有人知道是否可以通过使用指针顺序访问单个成员?
Typedef record_data {
float a;
float b;
float c;
}records,*Sptr;
records lists[5];
Sptr ptr;
示例:为 5 个列表的所有成员分配 float 1.0 的值
// instead of this
(void)testworks(void){
int i;
float j=1.0;
ptr = &lists[i]
ptr->lists[0].a = j;
ptr->lists[0].b = j;
ptr->lists[0].c = j;
ptr->lists[1].a = j;
// ... and so on
ptr->lists[4].c = j;
}
// want to do this
(void)testwannado(void){
int a,i;
float j=1.0;
ptr = &lists[i]
for (a=0;a<5;a++){ // step through typedef structs
for (i=0;i<3;i++){ // step through members
???
}
}
请原谅我在下面这个例子中的错误,但它代表了我能想到的最接近我想要完成的事情。
int *mptr;
mptr = &(ptr->lists[0].a) // want to assign a pointer to members so all 3 members can be used...
*mptr++ = j; // so I can do something like this.
这不是编译的,所以任何其他错误都是无意的。