Update:
I consider this question to be distinctly different from the questions marked as possible duplicates because of my use of the keyword "const." The answers marked as duplicate do not adequately explain the implications of const in these cases. It seems to me that const should force the compiler to treat these two cases the same, even though without const they are different. See my comment below my question for more details.
What's the difference between const char myStr[] = "hello"
and const char* myStr = "hello"
?
When I compile the former, the compiled program size is 20 bytes more than the latter, though the two cases take the same amount of space for global variables. Compiler optimization is set to "-Os".
Update:
-As far as compiled program size goes, static const char myStr[] = "hello"
is identical to const char* myStr = "hello"
, which is identical to getting rid of the variable and just passing in the string literal "hello"
directly to the function as a parameter. const char myStr[] = "hello"
takes 20 bytes more than the other cases just mentioned. char myStr[] = "hello"
is identical in program size to its "const" counterpart.
I am passing this string to a function I wrote which requires const char str[]
as an input parameter.
Part 2: is the function requiring const char str[]
as an input parameter identical to one requiring const char* str
as an input parameter?
Related (but not the same) questions