still confused by your comments. why worry about where it is allocated? and why the discussion of nullptr?
while a compiler can store a const char in non-volatile memory, i don't believe that's the primary purpose of const char. as you've pointed out, I believe it is to generate a compile or run time error when there is an attempt to modify such a string.
i think there needs to be a clear understanding that anything defined within quotes, "" is a constant. an attempt to modify such a string (e.g. *p = 'G') results in an error, even in C
passing a const char to a sub-function that modifies a string defined as char * results in a run-time error while defining the argument as const char * results in a compile time error. this was my view on the value of const char
another behavior is defining const char ptr, e.g. const char *t = "hello"; prevents changing the value of the ptr
all 3 of the above behaviors are sensible (if you know what you're doing) and can prevent problems if just blindly used (if you're unsure)
in both cases: "" and "ab", the strings are null terminated and allocated in a section of memory that is not intended to be written. this space would be different that where other variables are allocated.
you can see that "p" has significantly different address values when set to "george" at 0x100403024 vs the char array s at 0xffffcbd0
s addr 0xffffcbd0 - 11 68 65 6c 6c 6f 20 77 6f 72 6c 64
p = 0 addr 0x0
p george addr 0x100403024 - 6 67 65 6f 72 67 65
p = s addr 0xffffcbd0 - 11 68 65 6c 6c 6f 20 77 6f 72 6c 64
s zeroed addr 0xffffcbd0 - 0
the compiler separates code ("text") from data ("data" and "bss"). one reason is to allow memory protection hardware to prevent code from overwriting code as well as outside the memory allocated for the task. and of course this allows the text segment to more easily be allocated to non-volatile memory (flash) on things like AVR processors.
back when ROM and RAM were used, the text segment has to be located within the address space of the ROM and the data in the RAM space which has to be explicitly specified to the linker for the particular hardware being used.
That’s a possible compiler decision depending on the underlying environment.
My point is just that you should not use char * for a pointer if you want to initialize it to pointing to something that is defined within double quotes (as this is const).
Exactly my point and what you should not do and that will raise a warning
(Yes it allocated 1 byte which is null but it’s constant)
the compiler reminds you of this with a warning (see post #16, "ISO C++ forbids).
but you may want to use a char * ptr if it needs to be changed. (i think this is comparable to "const int MyNum = 56;" which relies on the compiler to do what the preprocessor, "#define MyNum 56")
#include <stdio.h>
int
main ()
{
char *p = "george";
printf (" p %s\n", p);
*p = 'G';
printf (" p %s\n", p);
}
why?
what if i want a string point to have a default string value (e.g. "no value set") but is later set to a dynamically created string (e.g "value is 29").
Because the language says so. That was the point I was trying to make, if you want a non const data pointer, it needs to point to non const data so you define
char p[] = "George";
P still is a pointer to char but the char data is not constant
That’s was also the whole purpose of
char nullChar = '\0';
char * pempty = & nullChar;
nullChar is modifiable so I can use its address to initialize the pempty pointer