I am trying to understand one pointer example in the o'reilly "Practical C" book regarding "const pointers".
const char *answer_ptr = "Forty-Two";
answer_ptr = "Fifty-One"; /* Legal because the pointer is a var and not constant
*answer_ptr = 'X'; /* Illegal because defined as constant.
What I don't understand is, what exactly is the "Forty-One"? Where is it stored, and what can I used it for?
If I try it out in CodeBlocks:
const char *answer_ptr = "Forty-One";
printf("Pointer: %s, Content: %c, Address %p.\n", answer_ptr, *answer_ptr, &answer_ptr);
answer_ptr = "Twenty-Two";
printf("Pointer: %s, Content: %c, Address %p.\n", answer_ptr, *answer_ptr, &answer_ptr);
The output is:
Pointer: Forty-One, Content: F, Address 0060FF0C.
Pointer: Twenty-Two, Content: T, Address 0060FF0C.
So it seems I can change the content of the variable the pointer is pointing to, even that I defined it as "const".
Is my understanding correct or do I miss here an important point?
To be honest I am very confused since answer_ptr and *answer_ptr seem to be the same...
Thank you guys for your help! Cheers, Styx