| | |
problem about statement
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
Originally Posted by Sukhbir
PLS EXPLAIN ME.C++ Syntax (Toggle Plain Text)
char *ptr="abc"; char *ptr1; while(*ptr1++=*ptr++); //IT IS WORKING while((*ptr1)++=(*ptr)++); //ERROR
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
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.
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.
![]() |
Similar Threads
- having problem with an if statement... (C++)
- Problem on IF STATEMENT on TRIGGER with MySql (MySQL)
- Problem in using Map (C++)
- simple 'if statement' issue (C#)
- Number guessing game problem (C++)
- Question: Linear Time Sorting Problem (Computer Science)
- problem with "for" statement (C++)
- TMT Pascal Input Console problem (Pascal and Delphi)
- gcd problem (C++)
Other Threads in the C++ Forum
- Previous Thread: 128bit arithmetic
- Next Thread: Need Help counting Array Length
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






