What is the memory address for "hello world"
?
Noone can say that before the execution. The exact address of the string literal "hello world"
is determined at execution by the operation system and is furthermore dependent upon your implementation and ressources. A program has no influence on the exact location of a string literal in memory.
"How it apply to the pointer x
? Can anyone explain a little bit"?
char *x = "hello world";
x
is a pointer to char
(as you already know); "hello world"
is a string literal stored in read-only memory and isn't modifiable.
With this statement, the pointer x
get initialized by the address of the first element of this string literal, actually h
, inside of the read-only memory.
Since the string literal automatically evaluates to a pointer to its first element, there is no need to use the &
operator.
Take a look at:
What is the difference between char s[] and char *s?
https://en.cppreference.com/w/cpp/language/string_literal
https://en.wikipedia.org/wiki/String_literal
Side note:
- Make
x
a pointer to const char
(const char *x
) if it only shall point to string literals. In that way, the program will never invoke any undefined behavior by any unintentional write attempt to modify a string literal.