Connecting Tech Pros Worldwide Forums | Help | Site Map

Help for *x++=*y++;

Marco Stauder
Guest
 
Posts: n/a
#1: Jul 22 '05
What does this expression exactly do.
*x++=*y++;
x and y are both char pointers.
x points on p_ch which points on a C-string.
y indeed points directly on a C-Strings.

I know that the post-increment operator
has got a higher priority than the
dereference operator.

....
p_ch=cstring;
x=p_ch;
*x='\0';
*x++=*y++;
....

What happens with the '\0' (*x='\0'; ?
Does somebody know?

thx

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Help for *x++=*y++;


"Marco Stauder" <got.cha@gmx.de> wrote...[color=blue]
> What does this expression exactly do.
> *x++=*y++;
> x and y are both char pointers.
> x points on p_ch which points on a C-string.
> y indeed points directly on a C-Strings.
>
> I know that the post-increment operator
> has got a higher priority than the
> dereference operator.[/color]

Correct.

The chain of events is similar to

// -- beginning of the expression

char c = *y; // a temporary

y = y + 1; // not necessarily here, but certainly
// before the end of the original expr

*x = c;

x = x + 1;

// -- end of the expression
[color=blue]
>
> ...
> p_ch=cstring;
> x=p_ch;
> *x='\0';[/color]

This doesn't seem necessary.
[color=blue]
> *x++=*y++;
> ...
>
> What happens with the '\0' (*x='\0'; ?[/color]

What do you mean "what happens"?
[color=blue]
> Does somebody know?[/color]

Whoever wrote it probably knows.

Victor


Ron Natalie
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Help for *x++=*y++;



"Marco Stauder" <got.cha@gmx.de> wrote in message news:rgj000ti5n4r91gcinbrqf4dk4bq0nngu6@4ax.com...
\[color=blue]
> I know that the post-increment operator
> has got a higher priority than the
> dereference operator.[/color]

The word is precedence. Yes you are right.
*x++ means *(x++).
(not (*x)++).
[color=blue]
>
> ...
> p_ch=cstring;
> x=p_ch;
> *x='\0';[/color]
The above obliterated by the next line.[color=blue]
> *x++=*y++;[/color]

The expressions x++ and y++ both yield the original values of x and y
(before the increment), so one character is assigned from the where y
originally pointed to where x originally pointed. In addition, both x and
y are incremented to point at the next character.

char str[] = "0123456789";
x = str;
y = "abcdefghij";

*x++ = *y++;

moves the 'a'in y to the '0' in str.
x and y are incremented (so x is now str+1, pointing at the '1' and y is pointing at the b).

Daniel T.
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Help for *x++=*y++;


In article <rgj000ti5n4r91gcinbrqf4dk4bq0nngu6@4ax.com>,
Marco Stauder <got.cha@gmx.de> wrote:
[color=blue]
> What does this expression exactly do.
> *x++=*y++;[/color]


#include <iostream>
using namespace std;

void foo( char* x, const char* y )
{
char* start_of_x = x;
while ( *y ) {
cout << start_of_x << " " << (int)x << " " << (int) y << endl;
*x++ = *y++;
}
}

int main()
{
const char* b = "abcdefg";
char* a = new char[8];
// is the below strictly necessary? I don't think so but am not sure.
for ( int i = 0; i < 8; ++i )
a[i] = 0;
foo( a, b );
cout << a;
}

What does it look like "*x++ = *y++" is doing in the above?

[color=blue]
> x and y are both char pointers.
> x points on p_ch which points on a C-string.
> y indeed points directly on a C-Strings.
>
> I know that the post-increment operator
> has got a higher priority than the
> dereference operator.
>[/color]

I'm having to guess the types on the below. You have already stated that
x and y are char* so I'm assuming that p_ch and cstring are also char*'s.
[color=blue]
> ...
> p_ch=cstring;[/color]
now p_ch and cstring are both pointing to the same place.[color=blue]
> x=p_ch;[/color]
and so is x[color=blue]
> *x='\0';[/color]
now that one place that p_ch, cstring, and x is pointing to contains a
'\0'[color=blue]
> *x++=*y++;[/color]
now that place contains the same value that was contained by the place
that y was pointing to. p_ch and cstring still point to that place, but
x points to the next place.
[color=blue]
> What happens with the '\0' (*x='\0'; ?
> Does somebody know?[/color]

Nothing happens to the '\0'. Something happens to the place that x is
pointing to though, it is set to '\0'.
Lefteris Laskaridis
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Help for *x++=*y++;


Marco Stauder <got.cha@gmx.de> wrote in message news:<rgj000ti5n4r91gcinbrqf4dk4bq0nngu6@4ax.com>. ..
[color=blue]
> What does this expression exactly do.
> *x++=*y++;[/color]


say, char* x = "Hello";
char* y = "World";

x, and y both point to the first character of the string:

| H | E | L | L | O | /0| | W | O | R | L | D | /0|
*x *y

What happens if you itereate through this expression in a statement such as
while( *s2 ), the character pointed by y is copied to the location pointed
by x (the contents of this location are overwriten by *y).
Next both pointers are incremented to point to the next character, and
the process is repeated until the string pointed by y is copied in place of
the string pointed by x.

| W | O | R | L | D | /0| | W | O | R | L | D | /0|
*x *y

-
Lefteris
Ron Natalie
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Help for *x++=*y++;



"Lefteris Laskaridis" <celhellas@germanosnet.gr> wrote in message news:a9ec9f6d.0401120642.51b5e3fc@posting.google.c om...
[color=blue]
> say, char* x = "Hello";
> char* y = "World";[/color]
[color=blue]
> What happens if you itereate through this expression in a statement such as
> while( *s2 ), the character pointed by y is copied to the location pointed
> by x (the contents of this location are overwriten by *y).[/color]

Invoking undefined behavior in your case (can't modify string literals).

Lefteris Laskaridis
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Help for *x++=*y++;


[color=blue]
> Invoking undefined behavior in your case (can't modify string literals).
>[/color]

actually it tuns ok...



Ron Natalie
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Help for *x++=*y++;


Purely by coincidence. One of those subtle problems with undefined behavior
is that it appears to work in some cases.
"Lefteris Laskaridis" <celhellas@germanosnet.gr> wrote in message news:btv0t6$76g$2@newsmaster.public.dc.hol.net...
[color=blue]
> Invoking undefined behavior in your case (can't modify string literals).
>[/color]

actually it tuns ok...


Lefteris Laskaridis
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Help for *x++=*y++;


"Ron Natalie" <ron@sensor.com> wrote in message news:<40030d2a$0$2641$9a6e19ea@news.newshosting.co m>...
[color=blue]
> Purely by coincidence. One of those subtle problems with undefined
> behavior[/color]

it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.
Christoph Rabel
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Help for *x++=*y++;


Lefteris Laskaridis wrote:[color=blue]
> "Ron Natalie" <ron@sensor.com> wrote in message news:<40030d2a$0$2641$9a6e19ea@news.newshosting.co m>...
>[color=green]
>>Purely by coincidence. One of those subtle problems with undefined
>>behavior[/color]
>
> it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.[/color]

That it happens to "work" with some compilers is no proof
that it is correct.

For C-Style strings please use either
const char* x = "Hello";
or
char x[] = "Hello";

Christoph

P.S.:
Seg. fault with gcc 3.2 and with VC 7.1
Ali Cehreli
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Help for *x++=*y++;


Background:

We are talking about modifying string literals. For example:

int main()
{
char * p = "Hello";
*p = 'Y';
}

celhellas@germanosnet.gr (Lefteris Laskaridis) wrote in message news:<a9ec9f6d.0401130339.2f68db87@posting.google. com>...[color=blue]
> "Ron Natalie" <ron@sensor.com> wrote in message news:<40030d2a$0$2641$9a6e19ea@news.newshosting.co m>...
>[color=green]
> > Purely by coincidence. One of those subtle problems with undefined
> > behavior[/color]
>
> it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.[/color]

By "correctly," you must mean "as expected"; because there isn't *one*
correct way of running when undefined behavior is involved. For
example, the program above aborts with a 'Segmentation fault' when
compiled with gcc 3.2 and run on Linux. (Using gcc 2.95 doesn't make
any difference.)

String literals are actually arrays of constant characters. The C++
standard allows non-const pointers to be initialized with string
literals to avoid breaking old C code. Attempting to modify the string
literal through such a pointer is undefined behavior.

So, actually

char const * p = "Hello";

would be the correct definition of a pointer pointing to the first
constant character of the string literal.

Ali
Closed Thread