【发布时间】:2021-01-14 07:03:23
【问题描述】:
我正在尝试执行以下程序,但反过来编译器正在生成错误。我假设程序是正确的,并且我在程序的开头包含了函数原型。但是如果没有函数原型,程序就可以正常工作。 (即第一个函数定义和后来的主函数)。而且我想知道我的理解不正确的地方。
#include <stdio.h>
void func(struct tag v);
struct tag {
int i;
char c;
};
/*
void func(struct tag v)
{
printf("%d %c\n", v.i, v.c);
return;
}
*/
int main()
{
/*struct tag {
int i;
char c;
};*/
struct tag var = {2, 's'};
func(var);
return 0;
}
void func(struct tag v)
{
printf("%d %c\n", v.i, v.c);
return;
}
输出:
root@bhaskar:/home/bhaskar/Downloads/practice/cpractice/cindepth/struct# gcc p11_excer_1_4.c
p11_excer_1_4.c:3:18: warning: ‘struct tag’ declared inside parameter list will not be visible outside of this definition or declaration
void func(struct tag v);
^~~
p11_excer_1_4.c: In function ‘main’:
p11_excer_1_4.c:17:7: error: type of formal parameter 1 is incomplete
func(var);
^~~
p11_excer_1_4.c: At top level:
p11_excer_1_4.c:22:6: error: conflicting types for ‘func’
void func(struct tag v)
^~~~
p11_excer_1_4.c:3:6: note: previous declaration of ‘func’ was here
void func(struct tag v);
^~~~
【问题讨论】:
-
记得在使用结构之前声明结构。
-
请使用一致的缩进样式。
标签: c struct parameters scope namespaces