我正在编写一个c程序,我不太确定如何将数据写入结构的特定成员。我使用了一个struct指针,它使用realloc()获得分配的内存,并访问类似于数组的数据。每次添加元素时,都会动态调整数组的大小。
我的问题是,当将数据写入数组索引时,正确的方法是什么?
struct s{...some data members...}
struct s *s_array = NULL;
//allocate memory at some point using realloc()
printf("enter some data: ");
scanf("%d", &s[index].data_member);或
scanf("%d", &(s[index].data_member));发布于 2017-07-04 04:08:33
数组索引运算符[]和成员访问运算符.的优先级都高于address-of运算符&。
所以&s[index].data_member对于获取数组元素的成员地址是有效的。&(s[index].data_member)中的括号是多余的,不需要。
https://stackoverflow.com/questions/44893189
复制相似问题