I'm a beginner and I absolutely can't figure out how some string returns a pointer to its location in memory?
char* str = "okay"
why is it a pointer? After all, we don't even use new
I'm a beginner and I absolutely can't figure out how some string returns a pointer to its location in memory?
char* str = "okay"
why is it a pointer? After all, we don't even use new
how is char able to store an address
It doesn't, it's a pointer to a char
.
how some string returns a pointer to its location
String literals are (generally) constant character arrays stored in a read-only section in memory that's mapped to a section in your executable. More details are OS-specific, but what's important is that string literals decay into a pointer to their 1st character, just like any other array.
After all, we don't even use
new
new
and malloc()
return pointers, sure, but they're not the only source of pointers. As you see in your code, string literals are a source of pointers as well, and nothing stops you from writing stuff like char *p = (char *)0x800;
if you want, which is something you see often in kernel or real-mode programming.