【发布时间】:2014-03-26 23:50:26
【问题描述】:
我的代码给出了错误的答案,所以我将其精简为这一点。 当我的变量arrayCounter=0 时,它会在数组中返回正确答案。 但是当arrayCounter=1时,我得到的错误答案是0。
#include <iostream>
using namespace std;
struct base
{
int x,y;
};
struct myStruct : base
{
char c;
int numOne;
}; myStruct MS[10]; //array of myStruct
base * returnArray(char c) //function that returns array type
{
if(c=='m'){ return MS;}
//I plan to have other structs here similar to myStruct.
}
int main()
{
MS[0].x=204; //init 0 value in array
MS[1].x=97; //init 1 value in array
int arrayCounter=0; //count through array. if=0, returns correctly. If=1, then not correct...
cout<<returnArray('m')[arrayCounter].x<<endl; //MS[1].x=204, MS[1].x=0
system("pause");
}
【问题讨论】: