简体   繁体   中英

static const pointer to global volatile

How can I declare a static const pointer to global volatile?

I have this so far, but I'm not sure it's correct:

// 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;

Edit: I use it like this:

if (*flag_it_ptr) {
        // work
        *flag_it_ptr = false;
}

For those wondering why I am using that pointer: I may change the variable I'm using from compilation to compilation, and didn't want to be changing names across the whole file, so this way I change it once. More or less like a macro or a const global variable.

Is this correct?

Edit: It compiled on gcc

That construct just declares the const pointer to the not const object. So you are allowed to change the referenced object but not the pointer itself.

 #define flag_it_ptr flag_it_1 

will do the job without the pointers. I think you over complicate the simple things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM