[已解决]C99标准文档中看到的一段例子 不理解
本帖最后由 Third-Edition 于 2016-08-15 12:35 编辑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);
}
上文是C99文档6.5.2.3 Structure and union members的例子, 不懂 谁给解释解释为啥非法?
because the union type is not visible within function f这个解释完全没法理解
http://bbs.chinaunix.net/thread-1055182-1-1.html 意思是 f()函数并不清楚 入参是union 联合体,编译应该不会有问题 但会导致的结果是p1 p2指向同一块空间,当然出来的结果也是有问题的。
楼上正解,p1和p2指向相同的地址,而f并不知道,这会造成问题。 语法有效,而语义无效的一个例子...g到底想要干什么呢
页:
[1]