[OT]请问下,c语言中有类似这样的语法吗?

19 views
Skip to first unread message

kuyuxin zhang

unread,
May 15, 2013, 10:22:02 PM5/15/13
to sh...@googlegroups.com
问个很初级的问题。原因是这样的,我看到我这里有一个项目里的头文件是这样声明的:
typedef struct AA
{
struct BB
{
char a;
char b;
char c;
}bbstruct;
} aastruct;
typedef struct CC
{
        struct BB cc;
}ccstruct;
这个以前也没见过,这种写法不是标准c里面的吧?还是由编译器决定的?
PS:编译器我还不清楚,这个是一个外包项目,环境也是别人搭建好的。不像标准的linux系统。

Yongwei Wu

unread,
May 15, 2013, 11:02:01 PM5/15/13
to sh...@googlegroups.com
这应该是完全合法的。需要注意的是,C里面的作用域规则。

6.2.1 Scopes of identifiers

2 ... There are four kinds of scopes: function, file, block, and
function prototype. (A function prototype is a declaration of a
function that declares the types of its parameters.)

3 A label name is the only kind of identifier that has function scope.
It can be used (in a goto statement) anywhere in the function in
which it appears, and is declared implicitly by its syntactic
appearance (followed by a : and a statement).

4 Every other identifier has scope determined by the placement of its
declaration (in a declarator or type specifier). If the declarator
or type specifier that declares the identifier appears outside of
any block or list of parameters, the identifier has file scope,
which terminates at the end of the translation unit. If the
declarator or type specifier that declares the identifier appears
inside a block or within the list of parameter declarations in a
function definition, the identifier has block scope, which
terminates at the end of the associated block.

根据这些规则,aastruct、ccstruct和struct BB具有相同的作用域。如果定义
在一个块(如函数)外面的话,这些名字具有文件作用域。

当然这个风格并不好。名字也很糟糕:aastruct和ccstruct是类型,而bbstruct
是个结构成员的名字。

2013/5/16 kuyuxin zhang <zyongx...@gmail.com>:
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。
> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 shlug+un...@googlegroups.com
> 要查看更多选项,请访问 https://groups.google.com/groups/opt_out
>
>



--
Wu Yongwei
URL: http://wyw.dcweb.cn/

kevin lee

unread,
May 15, 2013, 11:34:42 PM5/15/13
to sh...@googlegroups.com
内核里经常见。。


--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 



--
Best Regards,
Kevin Lee

kuyuxin zhang

unread,
May 16, 2013, 5:33:52 AM5/16/13
to sh...@googlegroups.com
谢谢回复
Reply all
Reply to author
Forward
0 new messages