There is another thread on this but I could not see a solution other than ignore the warning. I don’t like compiling code with warnings. Can anyone help with a definitive solution please.
The code the produces this error is as follows:
#define footertextadminh "Admin Home "
and this function is declared as
void footerScreen(char *text)
I guess the question is what is the correct way to define the variable passed to footerScreen ?
Get rid of the define and use:
const char* footertextadminh = "Admin Home";
Your function expects a pointer, using the #define like that never creates a variable.
This would be a great textbook example of why we use const variables instead of #defines!
Thanks for the quick reply - I will avoid this ‘school boy error’ in future!
I have done as you suggested and now get 2 errors rather than a warning for each time this
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
error: initializing argument 1 of 'void footerScreen(char*)' [-fpermissive]
If the constant text string ‘variable’ is declared as a
const char* then presumably the function variable also needs to be
const char* ?
void footerScreen(const char* text)
I presume this works because I am not going to transform or operate on the text variable other than to use as an input to print?
One thing worth noting here is that this way
const applies only to the contents of the pointer and not to the pointer itself.
So you still could move the pointer to some other location, whereas if you did
const char footer = "Admin Home "; the array and its contents are located in flash and hence unalterable.
Given this fact the reason why your former warning turned into an error becomes more obvious, because even if you tried to alter the contents or the pointer your program must fail, since flash is no RAM.
or similarly use:
const char * const footertextadminh = "Admin Home";
so neither the pointer nor the string may be altered.
you just don’t see enough of that syntax, but it surely shows you your intent about the immutable nature (constness) of
footertextadminh if only by its eye-catching syntax.