为什么一个typedef不能在内部结构允许吗? [英] Why is a typedef not allowed in the inner struct?

查看:116
本文介绍了为什么一个typedef不能在内部结构允许吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,定义了现有typedef结构中的typedef结构,我想知道为什么我收到一个编译错误。

I have a program that defines a typedef struct within an existing typedef struct, and I am wondering why I am getting a compilation error.

下面是程序:

typedef struct Outer
{
    typedef struct Inner
    {
        int b;
    }INNER;


    INNER inner;
    int a; 

}OUTER;

int main()
{ 
    OUTER obj;
    obj.a = 10;
    obj.inner.b=8;
    return 0;
}   

在编译时给出以下错误:

on compilation gives following error ::

test.c:3:5: error:expected specifier-qualifier-list before ‘typedef’
test.c: In function ‘main’:
test.c:17:5: error: ‘OUTER’ has no member named ‘a’
test.c:18:5: error: ‘OUTER’ has no member named ‘inner’  

但是,当我改变了计划,以

but, when I changed the program to

typedef struct Outer
{
    struct Inner
    {
        int b;
    };

    struct Inner inner;
    int a; 

 }OUTER;

 int main()
 {
    OUTER obj;
    obj.a = 10;
    obj.inner.b=8;
    return 0;
 }   

它编译成功。

为什么不的typedef与内部结构允许吗?

Why is typedef not allowed with inner structures?

推荐答案

C没有允许存储类说明(的typedef ,也在一个结构成员的声明中静态的extern )。这是在结构和联合声明中6.7.2.1p1 C99中的语法规定。

C does not allow a storage-class specifier (typedef, but also static or extern) in the declaration of a structure member. This is specified in the syntax of structures and unions declaration in 6.7.2.1p1 in C99.

/* The compiler will issue a diagnostic for this declaration */ 
struct s {    
    static int a;
};

您可以比较6.7.2.1p1语法声明的语法,其中的声明符不是函数参数或在6.7p1结构/联合成员,并看到,在这种情况下,存储类说明符是允许的。

You can compare 6.7.2.1p1 syntax with the syntax of a declaration where the declarator is not a function parameter or a structure / union member in 6.7p1 and see that in this case the storage-class specifiers are allowed.

这篇关于为什么一个typedef不能在内部结构允许吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆