If I have the following
如果我有以下内容
struct a {
int a;
int b;
bool c;
}
and use the following initialization function
并使用以下初始化函数
struct ls *ls_new(struct ls **ls, size_t *size)
{
struct ls *m, *n;
n = realloc(*ls, (*size + 1) * sizeof(struct ls));
if (!n)
return NULL;
*ls = n;
m = *ls + *size;
(*size)++;
// init struct
*m = (struct ls) {.a = 0}
return m;
}
When I use ls_new()
to initialize an instance of struct a
the c99
standard guarantees that initializing one member of the struct will also initialize all other members. What will be the default initialization value for bool c
?
当我使用ls_new()初始化struct a的实例时,c99标准保证初始化struct的一个成员也将初始化所有其他成员。 bool c的默认初始化值是多少?
1 个解决方案
#1
3
What will be the default initialization value for bool c?
bool c的默认初始化值是多少?
It is 0
, i.e., false
.
它是0,即假。
From:
(C99, 6.7.8p22) "If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration."
(C99,6.7.8p22)“如果括号括起的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小的数组的字符串文字中的字符数少于对于数组,聚合的其余部分应与具有静态存储持续时间的对象隐式初始化。“
and
(C99, 6.7.8p10) "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then: [...]
(C99,6.7.8p10)“如果没有显式初始化具有自动存储持续时间的对象,则其值是不确定的。如果未明确初始化具有静态存储持续时间的对象,则:[...]
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- 如果它有算术类型,则初始化为(正或无符号)零;
And _Bool
(which bool
macros expands to) is an arithmetic type.
_Bool(bool宏扩展为)是一种算术类型。
#1
3
What will be the default initialization value for bool c?
bool c的默认初始化值是多少?
It is 0
, i.e., false
.
它是0,即假。
From:
(C99, 6.7.8p22) "If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration."
(C99,6.7.8p22)“如果括号括起的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小的数组的字符串文字中的字符数少于对于数组,聚合的其余部分应与具有静态存储持续时间的对象隐式初始化。“
and
(C99, 6.7.8p10) "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then: [...]
(C99,6.7.8p10)“如果没有显式初始化具有自动存储持续时间的对象,则其值是不确定的。如果未明确初始化具有静态存储持续时间的对象,则:[...]
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- 如果它有算术类型,则初始化为(正或无符号)零;
And _Bool
(which bool
macros expands to) is an arithmetic type.
_Bool(bool宏扩展为)是一种算术类型。