Search code examples
cargumentsvolatile

Avoid casting from volatile static uint8_t to uint8_t in function calls?


I currently have this code:

static void func( uint8_t var );

static volatile uint8_t foo;

int main() {  
 /* Here we have to cast to uint8_t */  
 func( (uint8_t) foo );

 /* This will not compile */  
 func( foo );  
}

Is there a way to avoid the cast in the function call?


Solution

  • I guess you are trying to pass a pointer to the variable and, while editing your question, you removed the pointer declaration in order to simplify, but that also changed your question. If you are passing a value to the function, there isn't any problem.

    Now, if you are passing a pointer, the volatile modifier tells that the compiler should expect the value of the variable to change by means other than the compiled code. You really shouldn't pass a volatile variable as a non-volatile parameter to a function. The function itself must be changed to have the volatile parameter, and recompiled. Then function (with volatile parameter) is prepared to deal with a volatile variable.