cpointersstaticconst-pointer

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


Solution

  • 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.