473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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?
Jul 19 '05 #1
3 3665
Angela wrote:
//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?

{ *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;
Jul 19 '05 #2
Angela wrote:
//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
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;

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.
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.

Jul 19 '05 #3
lilburne wrote:

[snip]


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

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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Casper | last post by:
In a recent posting, my notation was corrected: > tree* prootNode = new tree; > tree* psubNode = new tree; > > prootNode->child = psubNode; > psubNode->parent =...
9
by: PapaTodd | last post by:
Greetings all, I've been fighting with this issue all day. This code is tried and true on other OS's, but when I compile and test on AIX 5.3 using xlC_r, I have problems. Here is the code...
2
by: Foobarius Frobinium | last post by:
http://thelinuxlink.net/~fingolfin/pointer-guide/ I've made a lot of corrections, and put in more new stuff. I have malloc and pointers to struct's and maybe a few more things and I'm done. ...
8
by: Simon Morgan | last post by:
Can somebody please explain why the following piece of code generates an error unless I change q to *q: typedef int Queue; int top = 9; void insert(Queue *q, int n) { q = n; }
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
2
by: brzozo2 | last post by:
Hello, anyone knows if there is a way to use pointer notation instead of array when it comes to copying one object to another via a pointer of array to objects? Here is a very simple code to show...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
14
by: povoação | last post by:
can´t understand why the program above not works. thanks all #include <stdio.h> #include <stdlib.h> #define SIZE 3 struct peoples
3
by: Gary Wessle | last post by:
Hi class myType{ public: void doit(); std::string x; }; int main() { myType* mt;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.