Reputation: 95
Consider the following program:
#include <iostream>
using namespace std;
int main()
{
const char* p = "12345";
const char **q = &p;
*q = "abcde";
const char *s = ++p;
p = "XYZWVU";
cout << *++s;
return 0;
}
I have worked out the pointer arithmetic, and I have figured out that s points to the third character of p. However, on executing this program, it prints 'c' (which is the third character of abcde) instead of 'Z'. My doubt over here is that in the line
p = "XYZWVU";
We had made the pointer p point to the first character of string (XYZWVU), then why does this program not print 'Z'?
Please guide.
Upvotes: 0
Views: 313
Reputation: 21
Your code involves two types of addresses: addresses of memory locations and addresses stored at these memory locations.
For instance, pointer variable p is located at a specific address in memory, let's say 0x1234. Your code assigns this address to q. The value of p (the one which you have previously written to p), let's say 0xAABB, is the address stored at memory location 0x1234. Suppose the content of memory location 0x1234 changes, either by overwriting p or by writing to *q. It becomes, let's say, 0xAACC. This change is visible in *q and p, respectively, because q points to the address where p is located (the value of q is 0x1234). Variables p and q are connected: *q is an alias to p.
Pointer variable s is also located at a specific address, let's say 0x5678. But by assigning ++p to s, you merely store address 0xAACD to memory location 0x5678. Memory location 0x5678 is connected to memory location 0xAACD (because of this assignment), but not to memory location 0x1234. Subsequent changes to p (the content of memory location 0x1234) will not affect s (the content of memory location 0x5678). They are not connected.
It doesn't matter if p and s are pointer variables. In the context of redefining a variable, they behave just like scalar variables. It's just that they contain addresses instead of scalar values. If you assign an expression containing p to s, but then redefine p, the change cannot possibly be propagated to the value of s.
Upvotes: 0
Reputation: 311028
Consider this code snippet
const char* p = "12345";
const char **q = &p;
*q = "abcde";
const char *s = ++p;
before the declaration of the variable s
the value of the pointer p was changed using the pointer q
*q = "abcde";
In fact this statement is equivalent to
p = "abcde";
because dereferencing the pointer *q we get the pointer p.
So after initialization of the pointer s
const char *s = ++p;
it points to the second character of the string literal "abcde"
.
This statement
p = "XYZWVU";
does not influence on the variable s. The pointers s and p are independent on each other.
So this statement
cout << *++s;
outputs the third character of the string literal "abcde".
You should understand that pointers p and s occupy different extents of memory. They are different objects.
In this declaration
const char *s = ++p;
the value of the expression ++p is assigned to the pointer s. This value is the address of the second character of the string literal "abcde". So you may reassign the pointer p but the pointer s will still point (will have the same assigned value) to the second character of this string literal that has static storage duration. The string literal did not disappeared after reassigning the pointer p. That is the string literal exists independent on whether you changed the pointer p or not.
Upvotes: 1
Reputation: 75062
#include <iostream>
using namespace std;
int main()
{
const char* p = "12345"; // have p point at "12345"
const char **q = &p; // have q point at p
*q = "abcde"; // change what q points at (p) to "abcde"
const char *s = ++p; // increment p and assign its result ("bcde") to s
p = "XYZWVU"; // have p point at "XYZWVU"
cout << *++s; // increment s and dereference the result ("cde")
return 0;
}
p = "XYZWVU";
is done after the value of p
is assigned to s
, so it won't affect the value of s
.
Upvotes: 1