亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
12下一頁(yè)
最近訪問板塊 發(fā)新帖
查看: 4726 | 回復(fù): 11
打印 上一主題 下一主題

第二個(gè)關(guān)于C99標(biāo)準(zhǔn)的疑問? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-02-21 11:34 |只看該作者 |倒序?yàn)g覽
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在函數(shù)f中是否可見與f有什么關(guān)系呀
不明白
specifiers (6.7.2.1)

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-02-21 12:17 |只看該作者
union的定義在g()內(nèi),所以對(duì)于f()來說是不可見的,在f()中使用union中的元素是不行的。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2008-02-21 12:24 |只看該作者
int f(struct t1 *p1, struct t2 *p2)
中并沒有設(shè)及關(guān)于union中的任何東西。在f看來所有的都只是struct

還有在用gcc編譯時(shí)也不會(huì)出現(xiàn)任何問題

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2008-02-21 14:53 |只看該作者
今天各位老大不知道去哪里了,是不是都去過元宵節(jié)了,祝大家元宵節(jié)快樂

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2008-02-21 15:08 |只看該作者
原帖由 zhangjiakouzf 于 2008-2-21 12:24 發(fā)表
int f(struct t1 *p1, struct t2 *p2)
中并沒有設(shè)及關(guān)于union中的任何東西。在f看來所有的都只是struct

還有在用gcc編譯時(shí)也不會(huì)出現(xiàn)任何問題


我也試了, 也沒有報(bào)錯(cuò).

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2008-02-21 15:45 |只看該作者
可以編譯,但是邏輯有問題,因?yàn)楹瘮?shù)f的2個(gè)參數(shù)實(shí)際指向同一個(gè)對(duì)象

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2008-02-21 16:21 |只看該作者
原帖由 ytl 于 2008-2-21 15:45 發(fā)表
可以編譯,但是邏輯有問題,因?yàn)楹瘮?shù)f的2個(gè)參數(shù)實(shí)際指向同一個(gè)對(duì)象


我覺得c99里面所指的應(yīng)該不是邏輯問題,因?yàn)槿绻沁壿媶栴}的話也應(yīng)該是return f(&u.s1, &u.s2);這句,因?yàn)檫壿嬪e(cuò)誤是這句引起的。不關(guān)g函數(shù)什么事情呀。

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2008-02-22 10:21 |只看該作者
沒人理?

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 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.

就在說這個(gè)問題. 沒有說如果看不到declaration時(shí)的行為...

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2008-02-23 14:53 |只看該作者
對(duì)于 int f(struct t1 *p1, struct t2 *p2) 這樣聲明的函數(shù), C 的別名規(guī)則(Aliasing Rule)允許編譯器作這樣的假設(shè)前提:p1 和 p2 不指向同一對(duì)象,或者指向的對(duì)象不存在重疊。根據(jù)這個(gè)假設(shè)前提,編譯器可以對(duì)函數(shù)代碼進(jìn)行優(yōu)化(比如把 p2 指向的對(duì)象放到寄存器中)。在此情況下,f(&u.s1, &u.s2) 函數(shù)調(diào)用中兩個(gè)參數(shù)指針由于實(shí)際指向同一對(duì)象,其結(jié)果有可能是不正確的。

如果在函數(shù)范圍內(nèi) union u 的定義可見,則編譯器可根據(jù)關(guān)于 union 的這一特殊保證(5 One special guarantee..)判斷出有可能函數(shù)參數(shù) p1 和 p2 指向同一對(duì)象 union u 的成員,從而不會(huì)對(duì)函數(shù)進(jìn)行上面提到的優(yōu)化,以保證函數(shù)的結(jié)果正確。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP