C++将结构的数组作为函数参数传递
C++ Passing an array of struct as function argument
C++:下面显示的代码与在函数声明和定义中同时省略关键字struct
之间有什么区别?
#include <iostream>
#include <string>
struct student{
int age;
int number;
};
void printme( struct student m[]); // if 'struct' is omitted the code works as fine
int main()
{
student s[3];
s[0].age = 10;
s[0].number = 333;
printme(s);
return 0;
}
void printme( struct student m[]){
printf("George age and number: %d, %d n", m[0].age, m[0].number);
}
这来自C,在C中必须指定struct(或使用typedef)。在C++中,只要结构和对象不使用相同的id,就没有区别。请不要这样做,但如果你真的想这样做,那么你需要为类型写struct student
,为对象写student
(如果你真想混淆所有人,这不一定是学生类型)。基本上,C++编码标准倾向于建议跳过结构,并且永远不要对结构和对象使用相同的id。
请注意,您使用的是c++和,而不是c,所以这里没有区别,您可以同时使用这两个选项。
请检查以下内容:将结构传递给函数以获取更多信息。
相关文章:
- 如何在C++中将迭代器作为函数参数传递
- 为什么我不能将引用作为 std::async 的函数参数传递
- 节点插件 API 将数组作为函数参数传递
- 将字符串数组作为函数参数传递
- 如何通过 C++ 中的函数参数传递初始值设定项列表的固定大小的初始值设定项列表?
- 我可以动态创建新地图并作为函数参数传递吗?
- 将任意模板向量作为函数参数C++传递
- 将函数指针作为函数参数传递的目的
- C++:通过函数参数传递的值给出不同的结果
- 当数组大小在C++中未知时,如何在运行时将对字符串数组的引用作为函数参数传递?
- 通过宏将函数参数传递给函数
- C++:访问作为函数参数传递的双指针数组所指向的变量的值
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 将派生类构造函数参数传递给受保护的成员
- 将指向数组的指针作为函数参数传递,这本身就是另一个函数的返回值?
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 有什么理由C++ 11+ std::mutex 应该声明为全局变量,而不是作为函数参数传递到 std::thread 中
- 如何将派生类作为函数参数传递:如果派生类成员不在父类中怎么办
- 如何将变量作为构造函数参数或函数参数传递
- 当数组对象以函数参数传递时,为什么复制构造函数会自称