【发布时间】:2016-04-21 15:38:03
【问题描述】:
我正在尝试从函数传递结构数组。我搜索了很多,但无法找到解决方法。以下是我正在尝试的代码。
struct menuItemType
{
int itemNo;
string menuItem;
double price;
};
void getData(menuItemType *menuList[10])
{
menuList[0]->itemNo = 111;
menuList[0]->menuItem = "Apple";
menuList[0]->price = 2.00;
....
menuList[0]->itemNo = 120;
menuList[0]->menuItem = "Chocolate";
menuList[0]->price = 5.00;
}
int main()
{
/* i know that i can't return a array. but i want to get the menuList[10] values here.
not sure which code i have to use..*/
}
【问题讨论】:
-
抱歉输入问题。我的意思是 int main(){}
-
std::array 是可以从函数返回的数组的薄包装。
-
"我知道我不能返回一个数组" erm.. 不,你可以返回(几乎)任何东西,除非函数被声明为
void;) -
@tobi303 您不能在 C++ 中声明返回 C 样式数组的函数(实际上它可以返回指针)。
-
@Zereges 不,你不能。但是如果您可以返回
std::array或一些自定义包装器,谁需要它?
标签: c++ arrays function struct