filename is supposed to hold an address of a character (36 bits in core2Duo), why we are putting a 'string' in it ?
The sad truth is that we aren't. We are putting a pointer to the first character of the string literal in it. The string "file.txt"
is of type char[9]
, which, when assigned to a pointer, decays into char *
. But you should be assigning it to a pointer to const char
, since modifying it is illegal (results in undefined behavior). Read this.
Why the compiler would not generate an error since you might be storing an address which can never exist?
Excuse me, but what kind of never-existent address are you talking about? The address of the first character in the string literal is always explicitly valid!
(But even if it was -- the compiler knows very little, if any, about semantics. It simply cannot possibly always warn you if there's the possibility of using an invalid pointer. Sure, sometimes it can, but don't have high expectations.)