免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1649 | 回复: 2
打印 上一主题 下一主题

Structure and Union Declarations [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-11 06:41 |只看该作者 |倒序浏览
A very special rule applies to declarations of the form

    struct-or-union identifier;

that declare a structure or union, but have no declaration list and no declarators. Even if the identifier is a structure or union tag already declared in an outer scope (Par.A.11.1), this declaration makes the identifier the tag of a new, incompletely-typed structure or union in the current scope.


This recondite is new with ANSI. It is intended to deal with mutually-recursive structures declared in an inner scope, but whose tags might already be declared in the outer scope.


请问有朋友可以跟我说上面所说的是什么意思? 看很久都不太明白它的意思 , 可否用代码举例一下他的意思


谢谢

论坛徽章:
0
2 [报告]
发表于 2007-06-11 17:22 |只看该作者
比如下面的程序:
  1. struct s1 { int m; /* ... */ }; /* G1 */
  2. struct s2 { int n; /* ... */ }; /* G2 */

  3. int main( void )
  4. {
  5.   struct s2; /* P */
  6.   struct s1 { struct s2 *s2p; /* ... */ }; /* D1 */
  7.   struct s2 { struct s1 *s1p; /* ... */ }; /* D2 */

  8.   /* ... */

  9.   return 0;
  10. }
复制代码

如果只有 P 一句不存在,则 D1 中的 s2 指的是 G2 中的 s2。如果把 P 和 G2 都去掉,则 D1 中的 s2 指的是 D2 中的声明的 s2。可见,如果没有 P 的话,则 D1 中的 s2 是上下文敏感的——对它的解释取决于外层作用域中是否声明有 s2 标签。

如果有 P 一句,则不论在其外层范围中是否声明了 s2,D1 中的 s2 指的都是同一作用域中声明的 D2 中的 s2,从而消除了上下文敏感。这就是 struct-or-union identifier; 这样的前向声明的作用。

论坛徽章:
0
3 [报告]
发表于 2007-06-11 21:04 |只看该作者
原帖由 whyglinux 于 2007-6-11 17:22 发表
比如下面的程序:
[code]struct s1 { int m; /* ... */ }; /* G1 */
struct s2 { int n; /* ... */ }; /* G2 */

int main( void )
{
  struct s2; /* P */
  struct s1 { struct s2 *s2p; /* ... */ }; / ...

好,不愧为语言律师
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP