Pointers and strings?

Hello guys, so I was learning pointers and strings and I got very confused with the following part.
 
char* cString = "Some string";

so how can this work? I mean pointer is a memory location address to some variable right? So how can pointer type variable be a string? With the same logic this should also be working right? but it gives error:
 
int* nPointer = 5;
Last edited on
It's simply a fundamental difference in how the compiler handles "Some string" and 5. The meaning of the syntax is simply different, even though these cases look the same.

"Some string"
As far as the compiler is concerned, this is an instruction to create an array of char somewhere, containing those characters, and then get the memory address of the first char (i.e. a char*), and use that char*. This is what a c-style string is; an array of char somewhere in memory, and what's passed around is the memory address of the start of that array.

int* nPointer = 5;
In this case, what's on the right is not an instruction to the compiler to go making arrays in memory and then carry on with a pointer to it. It's just the number 5 (so this code doesn't work).
Last edited on
Topic archived. No new replies allowed.