C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do I write this in pointer notation?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Angela
Guest





PostPosted: Sun Oct 26, 2003 9:51 pm    Post subject: How do I write this in pointer notation? Reply with 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?
Back to top
lilburne
Guest





PostPosted: Sun Oct 26, 2003 10:49 pm    Post subject: Re: How do I write this in pointer notation? Reply with quote



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





PostPosted: Mon Oct 27, 2003 2:12 am    Post subject: Re: How do I write this in pointer notation? Reply with quote



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





PostPosted: Mon Oct 27, 2003 2:14 am    Post subject: Re: How do I write this in pointer notation? Reply with quote

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)

Quote:


--ag



--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group