【发布时间】:2012-02-17 08:11:44
【问题描述】:
我在将数组结构传递给函数进行处理时遇到问题。
我想我需要将数组的地址传递给函数,但是有多个编译器错误并且我用完了组合来尝试。
然后该函数需要向新的结构成员返回一个值。
这是我最好的镜头!
//-----------------
void Function(struct MyStruct* ptr);
//------------------
int main(){
MyStruct array[MAX];
for (int i=0; i<MAX; i++)
{
File>>array[i].V1;
File>>array[i].V2;
File>>array[i].V3;
File>>array[i].V4;
MyStruct* ptr = &array[i];
array[i].V5 = Function(ptr);
}
}
//-----------------------
void Function(struct MyStruct* ptr)
{
// do something with the struct, how to I access each element in here?
}
谢谢!
【问题讨论】:
-
您的
main看起来不错。不要忘记给Function一个有用的返回类型,它会匹配array[i].V5,在里面你可以通过ptr->V1、ptr->V2等指针访问MyStruct的成员
标签: c++ arrays function pointers struct