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

M

Marco Stauder

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
 
V

Victor Bazarov

Marco Stauder said:
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.

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
...
p_ch=cstring;
x=p_ch;
*x='\0';

This doesn't seem necessary.
*x++=*y++;
...

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

What do you mean "what happens"?
Does somebody know?

Whoever wrote it probably knows.

Victor
 
R

Ron Natalie

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

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

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

Daniel T.

Marco Stauder said:
What does this expression exactly do.
*x++=*y++;


#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 = 0;
foo( a, b );
cout << a;
}

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

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.

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.
...
p_ch=cstring;
now p_ch and cstring are both pointing to the same place.
x=p_ch; and so is x
*x='\0';
now that one place that p_ch, cstring, and x is pointing to contains a
'\0'
*x++=*y++;
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.
What happens with the '\0' (*x='\0'; ?
Does somebody know?

Nothing happens to the '\0'. Something happens to the place that x is
pointing to though, it is set to '\0'.
 
L

Lefteris Laskaridis

Marco Stauder said:
What does this expression exactly do.
*x++=*y++;


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
 
R

Ron Natalie

Lefteris Laskaridis said:
say, char* x = "Hello";
char* y = "World";
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).

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

Ron Natalie

Purely by coincidence. One of those subtle problems with undefined behavior
is that it appears to work in some cases.
Lefteris Laskaridis said:
Invoking undefined behavior in your case (can't modify string literals).

actually it tuns ok...
 
L

Lefteris Laskaridis

Ron Natalie said:
Purely by coincidence. One of those subtle problems with undefined
behavior

it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.
 
C

Christoph Rabel

Lefteris said:
it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.

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
 
A

Ali Cehreli

Background:

We are talking about modifying string literals. For example:

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

it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top