Pointer to Character Consant

Hello All,

I am confused about how a pointer to a character constant works.

Take the following as an example:

 
char *pt = "Hello World!";


I thought that when you initialize a value to a pointer you would normally initialize it to a location and not a value. I understand how the following would work for example:

1
2
int num;
int *pt = #


So here's my question: When you initialize a pointer to a character constant, where is the memory allocated from? Is the memory location determined automatically?
I thought that when you initialize a value to a pointer you would normally initialize it to a location and not a value.

Variables always hold values. An address happens to be one. In the case of a string literal, the literal evaluates to the address of the literal.

So here's my question: When you initialize a pointer to a character constant, where is the memory allocated from? Is the memory location determined automatically?

Note that you are not initializing a pointer to a character constant. You are initializing a pointer to the address of a string literal. Technically, the pointer type should be const char*

The address of the string literal is supplied and determined by the compiler/linker.



cire wrote:
The address of the string literal is supplied and determined by the compiler/linker.

(It will most likely be in the data segment of your application -- the spot where the compiler puts things like string constants.)
Topic archived. No new replies allowed.