problem about statement

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 31
Reputation: Sukhbir is an unknown quantity at this point 
Solved Threads: 0
Sukhbir Sukhbir is offline Offline
Light Poster

problem about statement

 
0
  #1
Sep 17th, 2004
char *ptr="abc";
char *ptr1;
while(*ptr1++=*ptr++); //IT IS WORKING
while((*ptr1)++=(*ptr)++); //ERROR

PLS EXPLAIN ME.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: problem about statement

 
0
  #2
Sep 17th, 2004
Originally Posted by Sukhbir
  1. char *ptr="abc";
  2. char *ptr1;
  3. while(*ptr1++=*ptr++); //IT IS WORKING
  4. while((*ptr1)++=(*ptr)++); //ERROR
PLS EXPLAIN ME.
[Added [code][/code].]

You haven't initialized ptr1: when you first dereference it, it is already undefined behavior, so "IT IS WORKING" is just plain "luck(?)".

In the "ERROR" line, the result of the ++ operator is not an lvalue, so it cannot be assigned to.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: problem about statement

 
0
  #3
Sep 17th, 2004
Sukhbir, you have a lot of interesting syntax questions, and they are all covered in one chapter of any good C book, because these questions all revolve around the way the C compiler (and thus the C++ compiler) has been specified to work. You can also go into the help for your compiler and look for 'precedence'; there is a nice table that lists the exact order specified by the language.

In your first example,

*ptr1++

is the same as

*(ptr1++)

to the compiler, thus your second example, where you supplied the parens, would be different. In general, when writing code like this that might confuse someone, it is nice to put in the parens explicitly, even if they are not strictly needed by the compiler.

Dave's comments are also right on.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC