473,387 Members | 1,492 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,387 software developers and data experts.

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

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
Jul 22 '05 #1
10 7215
"Marco Stauder" <go*****@gmx.de> wrote...
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
Jul 22 '05 #2

"Marco Stauder" <go*****@gmx.de> wrote in message news:rg********************************@4ax.com...
\
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).

Jul 22 '05 #3
In article <rg********************************@4ax.com>,
Marco Stauder <go*****@gmx.de> wrote:
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[i] = 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'.
Jul 22 '05 #4
Marco Stauder <go*****@gmx.de> wrote in message news:<rg********************************@4ax.com>. ..
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
Jul 22 '05 #5

"Lefteris Laskaridis" <ce*******@germanosnet.gr> wrote in message news:a9**************************@posting.google.c om...
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).

Jul 22 '05 #6
Invoking undefined behavior in your case (can't modify string literals).


actually it tuns ok...

Jul 22 '05 #7
Purely by coincidence. One of those subtle problems with undefined behavior
is that it appears to work in some cases.
"Lefteris Laskaridis" <ce*******@germanosnet.gr> wrote in message news:bt**********@newsmaster.public.dc.hol.net...
Invoking undefined behavior in your case (can't modify string literals).


actually it tuns ok...
Jul 22 '05 #8
"Ron Natalie" <ro*@sensor.com> wrote in message news:<40**********************@news.newshosting.co m>...
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.
Jul 22 '05 #9
Lefteris Laskaridis wrote:
"Ron Natalie" <ro*@sensor.com> wrote in message news:<40**********************@news.newshosting.co m>...
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.


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
Jul 22 '05 #10
Background:

We are talking about modifying string literals. For example:

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

ce*******@germanosnet.gr (Lefteris Laskaridis) wrote in message news:<a9**************************@posting.google. com>...
"Ron Natalie" <ro*@sensor.com> wrote in message news:<40**********************@news.newshosting.co m>...
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.


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
Jul 22 '05 #11

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.