是否可以在 c 中重新声明 const?
Is it possible to redeclare a const in c?
我遇到过一段代码,其中相同的 const 变量在 for 循环中被多次重新声明,如下所示:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct {
int integer;
bool boolean;
} mystruct_t;
void main (void) {
int i;
uint8_t *buf;
for (i = 0; i < 5; ++i) {
const mystruct_t mystruct = {
.integer = i,
.boolean = 0
};
memcpy(buf, &mystruct, sizeof(mystruct));
}
return;
}
c 标准允许这样做吗(我使用的是 c99)?如果是这样,在每次迭代中重新声明 struct 背后的动机是什么?
对于初学者,您不能使用关键字 struct
作为变量名。
const mystruct_t struct = {
^^^^^^
for 循环有自己的块作用域。当块作用域像此代码片段中那样递归地获取控件时
for (i = 0; i < 5; ++i) {
const mystruct_t struct = {
.integer = i;
.boolean = 0;
};
memcpy(buf, &struct, sizeof(struct));
}
然后mystruct_t
类型的常量变量被重新创建。
来自 C 标准(6.2.4 对象的存储持续时间)
6 For such an object that does not have a variable length array
type, its lifetime extends from entry into the block with which it is
associated until execution of that block ends in any way. (Entering
an enclosed block or calling a function suspends, but does not end,
execution of the current block.) If the block is entered
recursively, a new instance of the object is created each time. The
initial value of the object is indeterminate. If an initialization is
specified for the object, it is performed each time the declaration or
compound literal is reached in the execution of the block;
otherwise, the value becomes indeterminate each time the declaration
is reached.
所以这两个变量都没有在您的代码片段中重新声明。在 for 循环的每次迭代中,都会创建一个具有相同名称的变量的新实例。该变量使用限定符 const
声明,因为它不会在 for 语句的块范围内更改。由于变量不在 for 语句之外使用,因此它在使用它的最小范围内声明。
我遇到过一段代码,其中相同的 const 变量在 for 循环中被多次重新声明,如下所示:
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct {
int integer;
bool boolean;
} mystruct_t;
void main (void) {
int i;
uint8_t *buf;
for (i = 0; i < 5; ++i) {
const mystruct_t mystruct = {
.integer = i,
.boolean = 0
};
memcpy(buf, &mystruct, sizeof(mystruct));
}
return;
}
c 标准允许这样做吗(我使用的是 c99)?如果是这样,在每次迭代中重新声明 struct 背后的动机是什么?
对于初学者,您不能使用关键字 struct
作为变量名。
const mystruct_t struct = {
^^^^^^
for 循环有自己的块作用域。当块作用域像此代码片段中那样递归地获取控件时
for (i = 0; i < 5; ++i) {
const mystruct_t struct = {
.integer = i;
.boolean = 0;
};
memcpy(buf, &struct, sizeof(struct));
}
然后mystruct_t
类型的常量变量被重新创建。
来自 C 标准(6.2.4 对象的存储持续时间)
6 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.
所以这两个变量都没有在您的代码片段中重新声明。在 for 循环的每次迭代中,都会创建一个具有相同名称的变量的新实例。该变量使用限定符 const
声明,因为它不会在 for 语句的块范围内更改。由于变量不在 for 语句之外使用,因此它在使用它的最小范围内声明。