Re: simple pointer question
"lokman" <lokman@fagi.ne t> wrote in message
news:c5t70i$n71 $1@nn-tk105.ocn.ad.jp ...[color=blue]
> Hi and thanks for the great help.
>
> I have modified the simple code a little bit based on the reply.
>
> And I have a question again. I know that instead of using pointer
> arithmetic, I can access individual characters in this way: e.g. cout <<
> p[5]; This will simply print the 5th character in the character array.
> (Correct me if I am wrong please.).[/color]
It will actually print the 6th character .. p [0] prints the first, p
[1] prints the second and so on
[color=blue]
> My question is: Can someone please tell me what is the reason that if I[/color]
do:[color=blue]
> cout << p[5] << endl; before the while loop, it prints the charater "p",[/color]
as[color=blue]
> expected. But somehow, after the while loop, if I do : cout << p[5] <<[/color]
endl;[color=blue]
> again, it cannot print the "p" character anymore.
>
> Thanks a lot again.
>
>
> // Code starts below.
> #include <iostream>
>
> using namespace::std;
>
> int main() {
>
> const char *p = "test pointer";
>
> cout << p[5] << endl; // New piece of code
>
> while (*p) { cout << *p; ++p;} // Modified ! I understand ++pp is a good[/color]
Above you modifiy the pointer, ie the address it is pointing to.
[color=blue]
> practice now, thanks !!
> cout << endl;
>
> cout << p[5] << endl; // New piece of code[/color]
That means, that in the line above, p [5] means something different than
the p [5] before the loop, because the value of p is different.
[color=blue]
> return 0;
> }[/color]
hth
--
jb
(replace y with x if you want to reply by e-mail)
"lokman" <lokman@fagi.ne t> wrote in message
news:c5t70i$n71 $1@nn-tk105.ocn.ad.jp ...[color=blue]
> Hi and thanks for the great help.
>
> I have modified the simple code a little bit based on the reply.
>
> And I have a question again. I know that instead of using pointer
> arithmetic, I can access individual characters in this way: e.g. cout <<
> p[5]; This will simply print the 5th character in the character array.
> (Correct me if I am wrong please.).[/color]
It will actually print the 6th character .. p [0] prints the first, p
[1] prints the second and so on
[color=blue]
> My question is: Can someone please tell me what is the reason that if I[/color]
do:[color=blue]
> cout << p[5] << endl; before the while loop, it prints the charater "p",[/color]
as[color=blue]
> expected. But somehow, after the while loop, if I do : cout << p[5] <<[/color]
endl;[color=blue]
> again, it cannot print the "p" character anymore.
>
> Thanks a lot again.
>
>
> // Code starts below.
> #include <iostream>
>
> using namespace::std;
>
> int main() {
>
> const char *p = "test pointer";
>
> cout << p[5] << endl; // New piece of code
>
> while (*p) { cout << *p; ++p;} // Modified ! I understand ++pp is a good[/color]
Above you modifiy the pointer, ie the address it is pointing to.
[color=blue]
> practice now, thanks !!
> cout << endl;
>
> cout << p[5] << endl; // New piece of code[/color]
That means, that in the line above, p [5] means something different than
the p [5] before the loop, because the value of p is different.
[color=blue]
> return 0;
> }[/color]
hth
--
jb
(replace y with x if you want to reply by e-mail)
Comment