【发布时间】:2012-11-25 04:35:11
【问题描述】:
以下编译并打印“字符串”作为输出。
#include <stdio.h>
struct S { int x; char c[7]; };
struct S bar() {
struct S s = {42, "string"};
return s;
}
int main()
{
printf("%s", bar().c);
}
显然,根据
,这似乎调用了未定义的行为C99 6.5.2.2/5 如果尝试修改函数的结果 在下一个序列点之后调用或访问它,行为是 未定义。
我不明白它在哪里说“下一个序列点”。这是怎么回事?
【问题讨论】:
-
你从哪里得知这是 UB?
-
下一个序列点位于完整表达式的末尾,也就是分号 (
;)。这里没有未定义的行为。 -
@Luchian:当我读到
or to access it after the next sequence point, the behavior is undefined时,我产生了疑问,如何在序列点之后访问函数调用的结果?
标签: c c99 undefined-behavior function-calls