Write your question here.
I am having trouble understanding this.
If a const char* c_string contains a string such as the one shown below, why
does advancing the pointer like below, actually advance the start of that string by one character.
I have shown the output above my code below.
To explain. If I have a C-String const char* my_cstring= "axdlpto";
and I advance the pointer my_cstring (*my_cstring++) why is the output then
"xdlpto";
I thought that if you advance something at a particular address by doing this:
*mypointer++, the value pointed at by *mypointer is increased by 1.
For example if int *mypointer = 5;
*mypointer++ = 6 ?
So how does the below work with my_cstring. When I use my_cstring++ why does it advance
the start position of that string?
I really don't understand how this works? Could somebody explain it?
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
axdlpto
a
xdlpto
x
dlpto
d
lpto
l
pto
p
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const char* str = "axdlpto";
cout << str << endl;
cout << *str << endl;
*str++;
cout << str << endl;
cout << *str << endl;
*str++;
cout << str << endl;
cout << *str << endl;
*str++;
cout << str << endl;
cout << *str << endl;
*str++;
cout << str << endl;
cout << *str << endl;
}
| |