I am writing some code for a graphical LCD driven by an ATmega328, using the Arduino build chain with Stino as my IDE. I have a function which formats and displays a number with a label. This is the function:
void displayNumber(float value, char* label)
I realise that both parameters could be const
ed, but to maintain compatibility with some other code, they are like this.
If I call the function as follows:
displayNumber(externalTemp, "MAX");
It works fine. I understand string literals behave strangely in that they can't be modified (undefined behaviour) but they are not declared as const char*
but char*
.
If I try using the ternary operator to pass an argument to the function:
displayNumber(externalTemp, animate10s?"MAX":"MIN");
I get a compiler error:
invalid conversion from 'const char*' to 'char*'
Why is the ternary operator const
ing my string?
The compiler specifically used is avr-gcc/avr-g++ version 4.3.2, the one bundled with Arduino Beta 1.5.6-r2.
There is (or was until recently) a deprecated conversion from string literal to char *
(without the const it would normally have), which is what lets the simple call work.
The ternary expression is not a string literal, so the conversion cannot be applied to it.
(Your best solution would be to make sure the function parameter is properly declared as taking const char *
.)
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.