struct作为返回类型的函数

时间:2014-07-26 17:02:15

标签: c

在阅读C traps and Pitfalls时,我遇到了以下代码段:

struct logrec{
    int date;
    int time;
    int code;  
}            // semicolon is missing

main()
{
..
..
}

请注意,在}之后缺少分号,这会使main()返回结构。

而不是询问main()返回struct的问题[因为main()返回int以外的其他行为是未定义的行为],所以我想问任何其他fun()的问题。例如

struct Abc{int a;} fun(){
    Abc a1;    //Error : Unknown type name 'Abc'
}

int main(){
.....
}

正如评论声明Abc a中提到的那样,错误未知类型Abc

所以我对此有两个问题:

  1. struct Abc的范围是什么?
  2. 如果我无法在fun()内声明任何类型Abc的变量,那么如何 我可以退回struct Abc吗?

1 个答案:

答案 0 :(得分:3)

这只是一个简单的语法错误。 Abcstruct Abc的类型不同。

变化:

struct Abc{int a;} fun(){
    Abc a1;    //Error : Unknown type name 'Abc'
}

为:

struct Abc{int a;} fun(){
    struct Abc a1;
} //^^^^^^
相关问题