忘记密码   免费注册 查看新帖 | 论坛精华区

ChinaUnix.net

  平台 论坛 博客 认证专区 大话IT 视频 徽章 文库 沙龙 自测 下载 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
12下一页
最近访问板块 发新帖
查看: 3385 | 回复: 11

第二个关于C99标准的疑问? [复制链接]

论坛徽章:
0
发表于 2008-02-21 11:34 |显示全部楼层
6.5.2.3 Structure and union members
Constraints
1 The first operand of the . operator shall have a qualified or unqualified structure or union
type, and the second operand shall name a member of that type.
2 The first operand of the -> operator shall have type ‘‘pointer to qualified or unqualified
structure’’ or ‘‘pointer to qualified or unqualified union’’, and the second operand shall
name a member of the type pointed to.
Semantics
3 A postfix expression followed by the . operator and an identifier designates a member of
a structure or union object. The value is that of the named member,82) and is an lvalue if
the first expression is an lvalue. If the first expression has qualified type, the result has
the so-qualified version of the type of the designated member.
4 A postfix expression followed by the -> operator and an identifier designates a member
of a structure or union object. The value is that of the named member of the object to
which the first expression points, and is an lvalue.83) If the first expression is a pointer to
a qualified type, the result has the so-qualified version of the type of the designated
member.
5 One special guarantee is made in order to simplify the use of unions: if a union contains
several structures that share a common initial sequence (see below), and if the union
object currently contains one of these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of the complete type of the union is
visible. Two structures share a common initial sequence if corresponding members have
compatible types (and, for bit-fields, the same widths) for a sequence of one or more
initial members.
6 EXAMPLE 1 If f is a function returning a structure or union, and x is a member of that structure or
union, f().x is a valid postfix expression but is not an lvalue.
7 EXAMPLE 2 In:
struct s { int i; const int ci; };
struct s s;
const struct s cs;
volatile struct s vs;
the various members have the types:
s.i int
s.ci const int
cs.i const int
cs.ci const int
vs.i volatile int
vs.ci volatile const int

8 EXAMPLE 3 The following is a valid fragment:
union {
struct {
int alltypes;
} n;
struct {
int type;
int intnode;
} ni;
struct {
int type;
double doublenode;
} nf;
} u;
u.nf.type = 1;
u.nf.doublenode = 3.14;
/* ... */
if (u.n.alltypes == 1)
if (sin(u.nf.doublenode) == 0.0)
/* ... */
The following is not a valid fragment (because the union type is not visible within function f):
struct t1 { int m; };
struct t2 { int m; };
int f(struct t1 *p1, struct t2 *p2)
{
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
int g()
{
union {
struct t1 s1;
struct t2 s2;
} u;
/* ... */
return f(&u.s1, &u.s2);
}
Forward references: address and indirection operators (6.5.3.2), structure and union

红色的部分为什么说“The following is not a valid fragment (because the union type is not visible within function f):”
union在函数f中是否可见与f有什么关系呀
不明白
specifiers (6.7.2.1)

论坛徽章:
0
发表于 2008-02-21 12:17 |显示全部楼层
union的定义在g()内,所以对于f()来说是不可见的,在f()中使用union中的元素是不行的。

论坛徽章:
0
发表于 2008-02-21 12:24 |显示全部楼层
int f(struct t1 *p1, struct t2 *p2)
中并没有设及关于union中的任何东西。在f看来所有的都只是struct

还有在用gcc编译时也不会出现任何问题

论坛徽章:
0
发表于 2008-02-21 14:53 |显示全部楼层
今天各位老大不知道去哪里了,是不是都去过元宵节了,祝大家元宵节快乐

论坛徽章:
0
发表于 2008-02-21 15:08 |显示全部楼层
原帖由 zhangjiakouzf 于 2008-2-21 12:24 发表
int f(struct t1 *p1, struct t2 *p2)
中并没有设及关于union中的任何东西。在f看来所有的都只是struct

还有在用gcc编译时也不会出现任何问题


我也试了, 也没有报错.

论坛徽章:
0
发表于 2008-02-21 15:45 |显示全部楼层
可以编译,但是逻辑有问题,因为函数f的2个参数实际指向同一个对象

论坛徽章:
0
发表于 2008-02-21 16:21 |显示全部楼层
原帖由 ytl 于 2008-2-21 15:45 发表
可以编译,但是逻辑有问题,因为函数f的2个参数实际指向同一个对象


我觉得c99里面所指的应该不是逻辑问题,因为如果是逻辑问题的话也应该是return f(&u.s1, &u.s2);这句,因为逻辑错误是这句引起的。不关g函数什么事情呀。

论坛徽章:
0
发表于 2008-02-22 10:21 |显示全部楼层
没人理?

论坛徽章:
0
发表于 2008-02-22 10:48 |显示全部楼层
5. One special guarantee is made in order to simplify the use of unions: if a union contains
several structures that share a common initial sequence (see below), and if the union
object currently contains one of these structures, it is permitted to inspect the common
initial part of any of them anywhere that a declaration of the complete type of the union is
visible. Two structures share a common initial sequence if corresponding members have
compatible types (and, for bit-fields, the same widths) for a sequence of one or more
initial members.

就在说这个问题. 没有说如果看不到declaration时的行为...

论坛徽章:
0
发表于 2008-02-23 14:53 |显示全部楼层
对于 int f(struct t1 *p1, struct t2 *p2) 这样声明的函数, C 的别名规则(Aliasing Rule)允许编译器作这样的假设前提:p1 和 p2 不指向同一对象,或者指向的对象不存在重叠。根据这个假设前提,编译器可以对函数代码进行优化(比如把 p2 指向的对象放到寄存器中)。在此情况下,f(&u.s1, &u.s2) 函数调用中两个参数指针由于实际指向同一对象,其结果有可能是不正确的。

如果在函数范围内 union u 的定义可见,则编译器可根据关于 union 的这一特殊保证(5 One special guarantee..)判断出有可能函数参数 p1 和 p2 指向同一对象 union u 的成员,从而不会对函数进行上面提到的优化,以保证函数的结果正确。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

【有奖调查】AI时代如何迎接未知挑战?

人工智能一直在医疗行业扮演着重要角色,最早的专家系统和后来的基因诊断都是人工智能技术在医疗的行业探索。近年来,随着深度学习等技术的进步,人工智能在医疗行业的应用领域不断扩展,医学影像智能诊断、语音电子病历、癌症智能诊断等均已逐渐成为热门发展方向。
而作为人工智能重要推动力的深度学习技术的快速发展却亟需认知系统的强力支撑。
----------------------------------------
活动时间:2017年5月15日-6月5日

调查入口>>
  

北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:1101082001
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP