[英]Getting a warning when passing struct through function
I'm trying to pass a whole structure through a function by value, and I test if the pass was successful by displaying the variables in the structure that was passed.我试图通过一个函数按值传递整个结构,并通过显示传递的结构中的变量来测试传递是否成功。
When I compile and run the program, the variables display correctly, but I get a warning:当我编译并运行程序时,变量显示正确,但我收到警告:
warning: parameter names (without types) in function declaration警告:函数声明中的参数名称(无类型)
So it refers to the function prototype being declared with parameter names that had no types?所以它指的是使用没有类型的参数名称声明的函数原型? But if I declared the function correctly, why am I getting this warning?但是,如果我正确声明了该函数,为什么会收到此警告?
Also, in the short program I wrote to test this, the warning doesn't affect the fact that the output being displayed is correct.此外,在我编写的用于测试的短程序中,警告不会影响显示输出正确的事实。
But if I were to get this warning under the same circumstances (passing struct through function) when writing a larger-scale program, will it affect the output in that program being correct?但是,如果我在编写大型程序时在相同情况下(通过函数传递结构)收到此警告,是否会影响该程序中的输出正确?
My code:我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void pass(Info);
typedef struct {
char str[10];
int num;
} Info;
void main() {
Info i;
strcpy(i.str, "Hello!");
i.num = 1;
pass(i);
}
void pass(Info i) {
printf("%s\n", i.str);
printf("%d\n", i.num);
}
Output:输出:
Hello!你好!
1 1
You have the typedef
statement after the usage of the newly defined type ( synonym for another type, to be nitpicky ).在使用新定义的类型(另一种类型的同义词,是 nitpicky )之后,您有typedef
语句。 At that point, in the function forward declaration, compiler does not know a type named Info
.此时,在函数前向声明中,编译器不知道名为Info
的类型。 So, it assumes it to be a variable name.因此,它假定它是一个变量名。
Move the typedef
before function prototype.将typedef
函数原型之前。
That said, void main()
is not a valid signature for main()
in a hosted environment.也就是说, void main()
main()
在托管环境中不是void main()
的有效签名。 It should be int main(void)
, at least.至少应该是int main(void)
。
Because you are declare the function prototype before the Info structure declaration.因为您在 Info 结构声明之前声明了函数原型。
An alternative could be the following code:另一种可能是以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char str[10];
int num;
} Info;
void pass(Info);
void main()
{
Info i;
strcpy(i.str, "Hello!");
i.num = 1;
pass(i)
}
void pass(Info i)
{
printf("%s\n", i.str);
printf("%d\n", i.num);
}
The compiler considers this function declaration编译器认为这个函数声明
void pass(Info);
as a function declaration with an identifier list.作为带有标识符列表的函数声明。 That is here Info
is identifier of the parameter not its type.那就是这里的Info
是参数的标识符而不是它的类型。
However according to the C Standard (6.7.6.3 Function declarators (including prototypes)):但是根据 C 标准(6.7.6.3 函数声明符(包括原型)):
3 An identifier list in a function declarator that is not part of a definition of that function shall be empty . 3 函数声明符中不属于该函数定义的标识符列表应为空。
SO the compiler issues a warning that the identifier list is not empty for the function declaration that is not a function definition.因此,编译器发出警告,指出对于不是函数定义的函数声明,标识符列表不为空。
warning: parameter names (without types) in function declaration
Either you should write要么你应该写
void pass();
Or you should place the typedef of the structure declaration before the function declaration and in this case the name Info
will denote a type.或者您应该将结构声明的 typedef 放在函数声明之前,在这种情况下,名称Info
将表示一个类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.