I'm learning about pointers in C and I've got a few questions. Here's a piece of code just for an example.
int var = 300;
char s[] = "Clang";
char *p = "Wonder";
I know that all the variables have their addresses in memory.
The variables like var
and s
and p
have their own addresses in memory.
But I wonder if constants have memory addresses as well.
Do 300
,"Clang"
, "Wonder"
themselves have memory addresses?
Only objects and functions have addresses in C. Named variables are objects. A string literal "Wonder"
used by itself is an object, an array of 7 characters (i.e. char[7]
) - the 6 visible characters and the terminating null character - and therefore may have an address. The literal "Clang"
is a borderline case here, strictly speaking it does not have an address because it is not an object but just a special initializer syntax.
The C model is quite unlike Python programming language, where
a = 300
a
is a name that has no address, whereas 300
is an object that has an address.
The may is because while C says that an object or function has an address, many compilers optimize code, creating an executable that does not follow the strict C abstract machine; therefore an object might have an address only if you observe it.