Is only in char *ptr = "Hello World"
a string literal or are both "Hello World"
string literals?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char array[] = { "Hello World" };
char *ptr = "Hello World";
printf( "%s\n", array );
printf( "%s\n", ptr );
printf( "%c\n", *array );
printf( "%c\n", *ptr );
printf( "%c\n", array[1] );
printf( "%c\n", ptr[1] );
return EXIT_SUCCESS;
}
# Hello World
# Hello World
# H
# H
# e
# e
"Hello world"
is always a string literal, no matter what you do with it.
char *ptr = "Hello World";
defines a pointer which points to space which is often not modifiable (so you should handle it like a const char *
and your compiler might actually complain if you don't).
char array[] = { "Hello World" };
creates the array on the stack so it's modifiable and roughly equal to char array[sizeof("Hello World")]; strcpy(array, "Hello World");