[try Beta version]
Not logged in

 
Advancing a C-string

Feb 6, 2017 at 11:34am
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;
}
Last edited on Feb 6, 2017 at 11:43am
Feb 6, 2017 at 12:00pm
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.


The pointer is incremented so that it points at the next thing in an array. const char* str = "axdlpto"; is really an array of char, so when you increment the pointer, you get the next char. If you had an array of double, it would point to the next double. Same story if it was an array of some arbitrarily large object (say 1 MB in size)

When you use cout << str << endl; the compiler is smart enough to know where the end of the string is, because it is implicitly null terminated '\0'.

When you do this cout << *str << endl; , you just get the char that str currently points to, because *str dereferences str, so the result is a char.
Feb 6, 2017 at 12:06pm
For example if int *mypointer = 5;
*mypointer++ = 6 ?


You need to be aware of the difference between dealing with a value, and dealing with a pointer.

*mypointer dereferences the pointer and yields the value.

The * has 2 meanings: 1 to declare a pointer variable; 2 to dereference that variable, to give it's value
Feb 6, 2017 at 12:33pm
Couple other points:
int *mypointer = 5; ... won't compile, conversion from int to int*
You could do instead:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()

{
    int x = 5;
    int* mypointer = &x;
    (*mypointer)++;//() around *mypointer essential
    std::cout << *mypointer << '\n';
}

Note that parentheses around *mypointer is essential on line 8, otherwise operator precedence would increment mypointer and then dereference giving unexpected result
Last edited on Feb 6, 2017 at 12:34pm
Feb 7, 2017 at 4:38am
Precedence! Forgot about this.. Thanks for that.. I completely understand now.
Topic archived. No new replies allowed.