免费注册 查看新帖 |

Chinaunix

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

[C] 一段奇怪的代码,类型没有声明,结果能编译通过 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-20 15:16 |只看该作者 |倒序浏览
代码如下:

  1. int main(int argc,char *argv[])
  2. {
  3.         struct sdfsfsdfsfd *p;
  4.         return 0;
  5. }
复制代码
这样的代码,能编译通过,没有任何头文件,应该不检查struct sdfsfsdfsfd ,在C语言标准里,不知道意味着什么?

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2011-10-20 15:28 |只看该作者
看不出有哪里可以阻挠编译器编译下去,你想说什么?

论坛徽章:
0
3 [报告]
发表于 2011-10-20 15:41 |只看该作者
应该是直接将你的结构体指针的定义给优化掉了吧,你试一试关掉所有的优化选项,再试一试。

论坛徽章:
0
4 [报告]
发表于 2011-10-20 15:41 |只看该作者
我的意思是,声明一个未知类型的指针,也能编译通过,就是说编译器只是记住这个指针的类型信息,而不管这个类型是否声明过。

论坛徽章:
0
5 [报告]
发表于 2011-10-20 16:06 |只看该作者
没有问题。此处仅告知编译器需要分配一个指针大小的内存空间,具体什么类型的无所谓(都不影响指针的大小)。当然你要进一步用这个指针去做点什么的时候,就需要知道类型的具体内容了,到时就肯定报错了。

论坛徽章:
0
6 [报告]
发表于 2011-10-20 16:38 |只看该作者
显然如果此处报错的话,像下面这样的用法(通常用于链表)就没法实现了:
struct SV
{
    int a;
    struct SV *next;
};

显然到定义指针next的时候,struct SV的定义依然是不完全的,如果定义一个指针需要知道类型的内容的话,这种方法就无法实现了。

论坛徽章:
0
7 [报告]
发表于 2011-10-20 18:02 |只看该作者
显然如果此处报错的话,像下面这样的用法(通常用于链表)就没法实现了:
struct SV
{
    int a;
     ...
liwangli1983 发表于 2011-10-20 16:38



    有道理。

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
8 [报告]
发表于 2011-10-20 19:55 |只看该作者
代码如下:这样的代码,能编译通过,没有任何头文件,应该不检查struct sdfsfsdfsfd ,在C语言标准里,不知 ...
rtcontrol 发表于 2011-10-20 15:16


这完全没问题,我们经常用这种方法封装结构。在外部引用结构指针,这种指针只能由我提供的例程处理。

论坛徽章:
0
9 [报告]
发表于 2011-10-20 20:43 |只看该作者
ISO C99 6.2.5 Types
1 The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. (An identifier declared to be an object is the simplest such expression; the type is specified in the declaration of the identifier.) Types are partitioned into object types (types that fully describe objects), function types (types that describe functions), and incomplete types (types that describe objects but lack information needed to determine their sizes).
20 Any number of derived types can be constructed from the object, function, and
incomplete types, as follows:
...
— A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’.
...
22 An array type of unknown size is an incomplete type. It is completed, for an identifier of that type, by specifying the size in a later declaration (with internal or external linkage). A structure or union type of unknown content (as described in 6.7.2.3) is an incomplete type. It is completed, for all declarations of that type, by declaring the same structure or union tag with its defining content later in the same scope.

论坛徽章:
0
10 [报告]
发表于 2011-10-20 20:54 |只看该作者
本帖最后由 幻の上帝 于 2011-10-20 20:55 编辑

……本来想发声明语法的,发现太啰嗦,算了。
其实关键是struct sdfsfsdfsfd *p;就是一条声明,同时声明了sdfsfsdfsfd和p。
依据(虽然忽略一堆语法可能会糊):
ISO C99
6.7.2.1 Structure and union specifiers
7 The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit. The struct-declaration-list is a sequence of declarations for the members of the structure or union. If the struct-declaration-list contains no named members, the behavior is undefined. The type is incomplete until after the } that terminates the list.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP