Reputation: 568
If cstring
is a pointer, then why can it get a value directly? Secondly, Why wasn't the *cstring
's result equal to whole of string? Third, cstring
is a non-constant pointer to a constant character, so why change its value and not change its address?
#include <cstdio>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
const char* cstring = "string";
cout << cstring << endl << *cstring << endl << &cstring << endl;
cstring = "foo";
cout << cstring << endl << *cstring << endl << &cstring << endl;
_getch();
return 0;
}
Upvotes: 1
Views: 146
Reputation: 7482
If cstring is a pointer, then why can it get a value directly?
The operator <<
of cout
for const char*
is specialized to behave that way. It will treat the pointer as a NULL terminated string and will print it instead of the pointer value. For different types you get different behaviour. For char*
you have the whole string printed.
Why wasn't the *cstring's result equal to whole of string?
That is because the type of *cstring is char
and again, operator <<
behaves correctly by just printing a single char. a const char*
is essentially an array of char.An array is essentially a pointer to the first element of the array. If you use the *
operator on a pointer you are accessing whatever the pointer points to. If it points to the first element, well, you get the first element.
Third,
cstring
is a non-constant pointer to a constant character, so why change its value and not change its address?
As you said, cstring
is a non-constant pointer to constant data. You cannot change the place it points to (it is a constant pointer), but you can substitute the content of the pointed data with other stuff. You point to the same location but the content of that cell changes.
Upvotes: 2
Reputation: 16876
If
cstring
is a pointer, then why can it get a value directly?
Anyone can get the value pointed at by a pointer by dereferencing the pointer. That's what happens when you do std::cout << cstring
. The proper overload gets chosen that prints the string represented by cstring
assuming that is correctly formed, null-terminated C-style string.
Secondly, Why wasn't the
*cstring
's result equal to whole of string?
cstring
is a const char*
, so *cstring
is a const char
. Pass that to std::cout
and it will call an overload that prints one char
. The function that is called internally doesn't even know that this is just one char
in a string.
Third,
cstring
is a non-constant pointer to a constant character, so why change its value and not change its address?
You can't change the address of a variable. cstring
is in a fixed place on the stack. You change the value of cstring
, which is the address of the string that it's pointing to (it is now pointing to a different string, which has a different address, "string"
of course stil has the same address).
What you probably wanted to try is this:
const char* cstring = "string";
std::cout << (void*)cstring << std::endl;
cstring = "foo";
std::cout << (void*)cstring << std::endl;
Now you can see the different addresses. One is the address of "string"
and one is the address of "foo"
.
Upvotes: 1