I was wondering why I would receive this error when I add the volatile keyword - I am new to embedded C but my understanding was that it just told the complier not to optimize the variable?
This code give me no errors
void display_text(char *dispString) { char myLcdBuffer[20]; U32 i; my_strcpy(myLcdBuffer, dispString); /* * Send the data to the LCD screen */ for (i = 0; i < strlen(myLcdBuffer); i++) { SER_PutCharLcd(myLcdBuffer[i]); } }
However if I add the volatile keyword to the char myLcdBuffer
void display_text(char *dispString) { volatile char myLcdBuffer[20]; U32 i; my_strcpy(myLcdBuffer, dispString); /* * Send the data to the LCD screen */ for (i = 0; i < strlen(myLcdBuffer); i++) { SER_PutCharLcd(myLcdBuffer[i]); } }
I get this error msg - error: #167: argument of type "volatile char *" is incompatible with parameter of type "const char *"
stackoverflow.com/.../argument-of-type-volatile-char-is-incompatible-with-parameter-of-type-const
Because it also limits that how compiler(?) accesses a variable.
Seems only apply to pointers.
This is fine.
#include <stdio.h> void func1(const int var) { printf("Var is %d\n", var); } int main(void) { volatile int var_main = 1234; func1(var_main); return 0; }
This is not.
#include <stdio.h> void func1(const int * var) { printf("Var is %d\n", *var); } int main(void) { volatile int var_main = 1234; volatile int * ptr_main = &var_main; func1(ptr_main); return 0; }
Just to be sure, try also:
int * volatile ptr_main;
I would expect no error there too.
volatile int * volatile ptr_main;
should again create that error message.
Why? With pointers there are two things, the pointer itself and the content the pointer is pointing to. Both can be volatile independently.
From the link above:
"It's because implicit conversions can add qualifiers to the target of pointer types, but not remove them. So if you want your function to be able to accept volatile and/or const qualified pointers, you must declare it with both:
void foo(const volatile char * data);"
<quote>From the link above: (etcetera etcetera)</quote>
Tapir,
You have provided a very good explanation and I am glad to say you are correct.
It fills me with pride to see that you have learnt how to interpret, understand and respond.
Well done. Five gold stars.
Always yo're freind,
Teacher Zeusti (retired)
"Stupid is as stupid does".
So you waited all the way until "Always yo're freind," before you decided to add some faked language errors.
<quote>So you waited all the way until "Always yo're freind," before you decided to add some faked language errors.</quote>
Nope. Check his name.
Remember back to what I said before [to you] regarding this style.