如何声明一个指向全局 volatile 的静态 const 指针?
到目前为止我有这个,但我不确定它是否正确:
// a.c
volatile bool flag_it_1;
volatile bool flag_it_2;
// a.h
extern volatile bool flag_it_1;
extern volatile bool flag_it_2;
// b.c
#include "a.h"
static volatile bool *const flag_it_ptr = &flag_it_1;
编辑: 我这样使用它:
if (*flag_it_ptr) {
// work
*flag_it_ptr = false;
}
对于那些想知道我为什么要使用该指针的人:我可能会在每次编译之间更改我正在使用的变量,并且不想在整个文件中更改名称,所以这样我就更改了一次。或多或少类似于宏或 const 全局变量。
这个对吗?
编辑: 它在 gcc 上编译