Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
Angela
Guest
 
Posts: n/a
Default How do I write this in pointer notation?

//array notation works***********
char temp[]

for(int x=0; x<strlen(temp); 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?
  #2  
Old July 19th, 2005, 08:40 PM
lilburne
Guest
 
Posts: n/a
Default Re: How do I write this in pointer notation?

Angela wrote:
[color=blue]
> //array notation works***********
> char temp[]
>
> for(int x=0; x<strlen(temp); 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?[/color]


{ *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;


  #3  
Old July 19th, 2005, 08:41 PM
Artie Gold
Guest
 
Posts: n/a
Default Re: How do I write this in pointer notation?

Angela wrote:[color=blue]
> //array notation works***********
> char temp[]
>
> for(int x=0; x<strlen(temp); 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[/color]

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;
[color=blue]
>
> I have played around with this and usually it just erases the first
> few letters.[/color]

Yes, due to the invocation of undefined behavior noted above.
[color=blue]
> If I wanted to do something like this with a C++ string,
> is this possible?[/color]

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.

  #4  
Old July 19th, 2005, 08:41 PM
Artie Gold
Guest
 
Posts: n/a
Default Re: How do I write this in pointer notation?

lilburne wrote:

[snip][color=blue]
>
>
> { *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;[/color]

Better than my original reply! (on this point)
[color=blue]
>
>[/color]
--ag


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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles