[英]C++ Passing an array of struct as function argument
C++: What's the difference between the code as shown below AND simultaneously omitting the keyword struct
in the functional declaration and definition -- code seems to work the same both ways ? 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);
}
This comes from C, where you had to specify struct (or use typedef). 这来自C,您必须在其中指定struct(或使用typedef)。 In C++, there's no difference as long as you don't use the same id for both a struct and an object.
在C ++中, 只要您不对结构和对象使用相同的ID,就没有区别。 Please, don't do it, but if your really want to, then you'll need to write eg
struct student
for the type and student
for the object (which is not necessarily of type student, if you really want to confuse everyone). 请,不这样做,但如果你真的想,那么你就需要编写如
struct student
的类型和student
为对象(这不一定类型的学生,如果你真的想迷惑大家) 。 Basically, C+ coding standards tend to recommend skipping struct and never having the same id for struct and object. 基本上,C +编码标准倾向于建议跳过struct,并且对于struct和object永远不要使用相同的id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.