让我们有一个 struct Structure_S,其中元素par1(Int)和par2[](int数组).在函数中,我们必须在 struct 的一个特定指标[m]上求出PAR2的总和?你是怎么做到的?
int function(struct structure_s *ps){
//something that determinates index
for(int j = 0; j < m; j++){
//and now how do I get to the structures[index].par2[j]
}
}
//the variables n and m are known beforehand
struct structure_s{
int par1;
int par2[n];
}
int main(void){
struct structure_s structures[m];
int sum = function(structures);
//etc.
不幸的是,他们没有在课堂上展示这一点,我找不到太多有用的信息.
我试过使用Like (*(ps + index)).par2[j]
,但它不起作用,它显示指针指向未定义的类型.我还想,也许当我把它传递给函数时,应该是&; struct ,但仍然是同样的错误.还有(*p)[index].par2[j]
和(*(p)[index]).par2[j]
,但不知道该用什么.你知道怎么做吗?
编辑,解决方案:我太蠢了,谢谢你们的帮助,知道指针可以像数组一样被注意到是很好的,问题是:没有在函数之前定义 struct .你每天都能学到一些新东西.