 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Angela Guest
|
Posted: Sun Oct 26, 2003 9:51 pm Post subject: How do I write this in pointer notation? |
|
|
//array notation works***********
char temp[]
for(int x=0; x
{ temp[x]=(temp[x]*2); } //simple char encoding
//how do I do the same thing with pointer notation**********
char* temp
while( *temp )
{ *temp++= (*temp)*2; } //simple char encoding
I have played around with this and usually it just erases the first
few letters. If I wanted to do something like this with a C++ string,
is this possible?
|
|
| Back to top |
|
 |
lilburne Guest
|
Posted: Sun Oct 26, 2003 10:49 pm Post subject: Re: How do I write this in pointer notation? |
|
|
Angela wrote:
| Quote: | //array notation works***********
char temp[]
for(int x=0; x
{ temp[x]=(temp[x]*2); } //simple char encoding
//how do I do the same thing with pointer notation**********
char* temp
while( *temp )
{ *temp++= (*temp)*2; } //simple char encoding
I have played around with this and usually it just erases the first
few letters. If I wanted to do something like this with a C++ string,
is this possible?
|
{ *temp++= (*temp)*2; } //simple char encoding
The *temp++ has the side effect of incrementing temp, so the
temp on the left is not the same as the temp on the right.
*temp++ *= 2;
|
|
| Back to top |
|
 |
Artie Gold Guest
|
Posted: Mon Oct 27, 2003 2:12 am Post subject: Re: How do I write this in pointer notation? |
|
|
Angela wrote:
| Quote: | //array notation works***********
char temp[]
for(int x=0; x
{ temp[x]=(temp[x]*2); } //simple char encoding
//how do I do the same thing with pointer notation**********
char* temp
while( *temp )
{ *temp++= (*temp)*2; } //simple char encoding
|
You cannot do it this way; the value of `temp' is being altered
without an intervening sequence point causing undefined behavior.
One correct option would be to mimic the array form, as in:
for (; *temp; temp++)
*temp = *temp * 2;
| Quote: |
I have played around with this and usually it just erases the first
few letters.
|
Yes, due to the invocation of undefined behavior noted above.
| Quote: | If I wanted to do something like this with a C++ string,
is this possible?
|
Yes. Certainly. (Look up `iterator' and `transform', for example)
HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.
|
|
| Back to top |
|
 |
Artie Gold Guest
|
Posted: Mon Oct 27, 2003 2:14 am Post subject: Re: How do I write this in pointer notation? |
|
|
lilburne wrote:
[snip]
| Quote: |
{ *temp++= (*temp)*2; } //simple char encoding
The *temp++ has the side effect of incrementing temp, so the temp on the
left is not the same as the temp on the right.
*temp++ *= 2;
|
Better than my original reply! (on this point)
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|