C语言中的函数声明和结构声明

par*_*ban 2 c struct declaration

int f(struct r);
struct r
{
int a;
int b;
};
Run Code Online (Sandbox Code Playgroud)

源文件中的上述代码段会引发错误

warning: its scope is only this definition or declaration,
which is probably not what you want for the line int f(struct r) 
Run Code Online (Sandbox Code Playgroud)

和以下代码片段位于同一源文件中但在函数定义之前 f(struct r)

struct r emp;
f(emp);
Run Code Online (Sandbox Code Playgroud)

给出错误

error:type of formal parameter 1 is incomplete for the line f(emp)
Run Code Online (Sandbox Code Playgroud)

但是当结构被替换时,同样的事情typedef,没有这样的错误......

此属性是否在函数声明中声明一个参数,然后才使用该参数?

Vla*_*lad 5

尝试其他订单:

struct r { int a; int b; };
int f(struct r);
Run Code Online (Sandbox Code Playgroud)

如果需要在结构之前声明函数,请使用前向声明:

struct r;
int f(struct r);
...
struct r { int a; int b; };
int f(struct r anR)
{
    return anR.a + anR.b;
}
Run Code Online (Sandbox Code Playgroud)

问题是编译期间编译int f(struct r);器没有看到你的结构,所以它创建了一些临时结构.稍后您对结构的声明是从编译器的角度来看的,与临时的无关.