c++ - Why does the ternary operator const my strings? -
i writing code graphical lcd driven atmega328, using arduino build chain stino ide. have function formats , displays number label. function:
void displaynumber(float value, char* label)
i realise both parameters const
ed, maintain compatibility other code, this.
if call function follows:
displaynumber(externaltemp, "max");
it works fine. understand string literals behave strangely in can't modified (undefined behaviour) not declared const char*
char*
.
if try using ternary operator pass argument function:
displaynumber(externaltemp, animate10s?"max":"min");
i compiler error:
invalid conversion 'const char*' 'char*'
why ternary operator const
ing string?
the compiler used avr-gcc/avr-g++ version 4.3.2, 1 bundled arduino beta 1.5.6-r2.
there (or until recently) deprecated conversion string literal char *
(without const have), lets simple call work.
the ternary expression not string literal, conversion cannot applied it.
(your best solution make sure function parameter declared taking const char *
.)
Comments
Post a Comment