【发布时间】:2023-03-11 12:08:01
【问题描述】:
是
struct datainfo info = { 0 };
同
struct datainfo info;
memset(&info, 0, sizeof(info));
有什么区别,哪个更好?
【问题讨论】:
-
从语义上讲,最终结果在所有当前运行的系统上都是相同的。
-
是一样的。一个是另一个的语法糖。
-
@alinsoar 您假设空指针由全零表示,浮点零由全零表示。
-
@RaymondChen:我相信在 C 语言中你可以假设空指针为 0。但在 C++ 中不能。
-
@Bathsheba C 不要求空指针由全位零表示。虽然空指针的写法是
0,但实现应该是convert0to whatever the internal representation is。这是not even required that null pointers of different types have the same internal representation。