5 ms·
"Notice we don't have char anymore. char is actually misnamed and misused in C." so defining a string happens how if not: char *my_string = "Hello";
by plg 11y ago
"Notice we don't have char anymore. char is actually misnamed and misused in C."
so defining a string happens how if not:
char *my_string = "Hello";
- adsche 11y agojust read on to the next paragraph...
- efaref 11y agoThat's not even correct. The type of string literals is "const char *".
- pbsd 11y agoTo be pedantic about it, string literals in C are of type `char[]`. In C++ they are of type `const char []`. In either case, altering the contents of the string literal is undefined behavior.
- plg 11y agoi didn't mean a const i meant a variable
- julian-klode 11y agoYour variable my_string is a pointer to a constant string. char * my_string It must be a const char * my_string You can still assign a different string to that, as my_string is not const. For my_string to be const, you'd have to use char * const my_string Now the location pointed to by my_string is writable but I cannot change the location. To make it entirely unmodifiable, you'd use const char * const my_string. On the other hand, it might make more sense to use an array here. const char my_string[] = "hello";