Thanks everyone! I really appreciate the advice and assistance!
Printable View
Thanks everyone! I really appreciate the advice and assistance!
To be precise, a string literal is not a pointer, but an array. For example "hello" would be an array of 6 chars, including the null character that is automatically appended. Now, this is often only a pedantic difference since arrays are converted to pointers to their first element whenever necessary. One difference arises with the use of sizeof, e.g., sizeof("hello") is 6, but sizeof(char*) may well be 4 (or whatever is the size of a pointer to char).
Another thing to note is that you can initialise a char array with a string literal, e.g.,
here, str can safely be an array of non-const char since it is not a string literal, unlike:Code:char str[] = "hello world";
where ptr points to the first char of the string literal "hello world". So, it is perfectly fine to assign to str[0], but assigning to ptr[0] results in undefined behaviour.Code:const char *ptr = "hello world";