结构体成员初始化之间是否存在序列点?
结构成员初始化表达式之间是否存在序列点?
例如,下面的代码是否总是打印“a,b”?
#include <stdio.h>
typedef struct {
char *bytes;
int position;
int length;
} Stream;
typedef struct {
char a;
char b;
} Pair;
char streamgetc(Stream *stream) {
return (stream->position < stream->length) ? stream->bytes[stream->position++] : 0;
}
int main(void) {
Stream stream = {.bytes = "abc", .position = 0, .length = 3};
Pair pair = {.a = streamgetc(&stream), .b = streamgetc(&stream)};
printf("%c, %c\n", pair.a, pair.b);
return 0;
}
Is there a sequence point between structure member initialization expressions?
For example, is it well defined that the code bellow will always print "a, b"?
#include <stdio.h>
typedef struct {
char *bytes;
int position;
int length;
} Stream;
typedef struct {
char a;
char b;
} Pair;
char streamgetc(Stream *stream) {
return (stream->position < stream->length) ? stream->bytes[stream->position++] : 0;
}
int main(void) {
Stream stream = {.bytes = "abc", .position = 0, .length = 3};
Pair pair = {.a = streamgetc(&stream), .b = streamgetc(&stream)};
printf("%c, %c\n", pair.a, pair.b);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为
§6.7.8-23
解决了这个问题:关于复合文字:
I think
§6.7.8-23
settles it:And about compound literals:
我相信C99 TC2(n1124)中的相关措辞位于§6.7.8/23中:
脚注说:
I believe the relevant wording in C99 TC2 (n1124) is in §6.7.8/23:
The footnote says:
不会。您可以在 C 标准的附录 C(或草案 n1256、n1516 等)中亲自查看。
每个完整声明符后面都有一个序列点,并且在初始化内部使用
&&
或调用函数的表达式中仍然存在序列点。函数参数之间也没有序列点。
No. You can see for yourself in Annex C of the C standard (or drafts n1256, n1516, etc.).
There is a sequence point after each full declarator, and there will still be sequence points from expressions inside the initialization that use
&&
or call functions.There isn't a sequence point between function arguments either.