post-increment

pointer increment and dereference (lvalue required error)

孤者浪人 提交于 2019-11-28 14:18:20
I am trying to understand how pointer incrementing and dereferencing go together, and I did this to try it out: #include <stdio.h> int main(int argc, char *argv[]) { char *words[] = {"word1","word2"}; printf("%p\n",words); printf("%s\n",*words++); printf("%p\n",words); return 0; } I expected this code to do one of these: First dereference then increase the pointer (printing word1) First dereference then increase the value (printing ord1) Dereference pointer + 1 (printing word2) But compiler won't even compile this, and gives this error: lvalue required as increment operand am I doing something

Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

谁说胖子不能爱 提交于 2019-11-28 11:57:56
问题 Why does this int x = 2; for (int y =2; y>0;y--){ System.out.println(x + " "+ y + " "); x++; } prints the same as this? int x = 2; for (int y =2; y>0;--y){ System.out.println(x + " "+ y + " "); x++; } As far, as I understand a post-increment is first used "as it is" then incremented. Are pre-increment is first added and then used. Why this doesn't apply to the body of a for loop? 回答1: The loop is equivalent to: int x = 2; { int y = 2; while (y > 0) { System.out.println(x + " "+ y + " "); x++;

a += a++ * a++ * a++ in Java. How does it get evaluated?

旧时模样 提交于 2019-11-28 11:17:34
I came across this problem in this website , and tried it in Eclipse but couldn't understand how exactly they are evaluated. int x = 3, y = 7, z = 4; x += x++ * x++ * x++; // gives x = 63 System.out.println(x); y = y * y++; System.out.println(y); // gives y = 49 z = z++ + z; System.out.println(z); // gives z = 9 According to a comment in the website, x += x++ * x++ * x++ resolves to x = x+((x+2)*(x+1)*x) which turns out to be true. I think I am missing something about this operator precedence. Java evaluates expressions left to right & according to their precedence. int x = 3, y = 7, z = 4; x

Multiple increment operators in single statement [duplicate]

微笑、不失礼 提交于 2019-11-28 10:44:15
问题 Possible Duplicate: Undefined Behavior and Sequence Points Pleae explain the behaviour of following statements int b=3; cout<<b++*++b<<endl; How will it be calculated? 回答1: The behavior here is undefined. See this question Relevant standard quote: §5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. The most common sequence point is the end of a statement. Also worth noting from the standard:

Difference in Increment-decrement operator in C and JAVA [duplicate]

夙愿已清 提交于 2019-11-28 08:55:37
问题 This question already has answers here : What is x after “x = x++”? (17 answers) Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 6 years ago . Please consider the following statement: int a[]={1,2,3,4,5,6,7,8}; int i=0,n; n=a[++i] + i++ + a[i++] + a[i] ; According to my logic n should be 10. But I am getting different output in c (output is 7) However in java I am getting expected result that is 10. Is there any difference in the way in which

Why can't I do ++i++ in C-like languages?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 07:18:43
Half jokingly half serious : Why can't I do ++i++ in C-like languages, specifically in C#? I'd expect it to increment the value, use that in my expression, then increment again. Short answer: i++ is not an "lvalue", so can't be the subject of an assignment. Eric Lippert Though the short answer "it's not an lvalue" is correct , that's perhaps just begging the question. Why isn't it an lvalue ? Or, as we say in C#, a variable . The reason is because you cannot have your cake and eat it too . Work it out logically: First off, the meaning of a ++ operator in C#, whether postfix or prefix, is "take

Post increment and Pre increment in C

不想你离开。 提交于 2019-11-28 07:03:40
问题 I have a question about these two C statements: x = y++; t = *ptr++; With statement 1, the initial value of y is copied into x then y is incremented. With statement 2, We look into the value pointed at by *ptr, putting that into variable t, then sometime later increment ptr. For statement 1, the suffix increment operator has higher precedence than the assignment operator. So shouldn't y be incremented first and then x is assigned to the incremented value of y? I'm not understanding operator

Why doesn't the post increment operator work on a method that returns an int?

China☆狼群 提交于 2019-11-28 05:37:48
public void increment(){ int zero = 0; int oneA = zero++; // Compiles int oneB = 0++; // Doesn't compile int oneC = getInt()++; // Doesn't compile } private int getInt(){ return 0; } They are all int's, why won't B & C compile? Is it to do with the way ++ operator differs from = 0 + 1; ? Invalid argument to operation ++/-- i++ is an assignment to a variable i . In your case, zero++ is an equivalent to zero = zero + 1 . So 0++ would mean 0 = 0 + 1 , which makes no sense, as well as getInt() = getInt() + 1 . More accurately : int oneA = zero++; means int oneA = zero; zero = zero + 1; // OK, oneA

Post Increment with respect to Sequence Points

喜夏-厌秋 提交于 2019-11-28 02:27:43
When does the post increment operator affect the increment? I have come across two opinions: 1) From http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_015.htm : POST means do the operation after any assignment operation. 2) Closer home, an answer on SO (albeit on C++) says: ... that delays the increment until the end of the expression (next sequence point). So does the post increment operation... A) wait until a sequence point is reached or B) happen post an assignment operator or C) happen anytime before the sequence point? The correct interpretation is C, ie. the increment happens

When does postincrement i++ get executed? [duplicate]

十年热恋 提交于 2019-11-28 02:09:34
Possible Duplicate: Undefined Behavior and Sequence Points In C++ on a machine code level, when does the postincrement++ operator get executed? The precedence table indicates that postfix++ operators are level 2: which means in int x = 0 ; int y = x++ + x++ ; // ans: y=0 The postfix ++'s execute first . However, it would seem that the logical operation of this line is the addition happens first (0+0), but how does that happen? What I imagine, is the following: // Option 1: // Perform x++ 2 times. // Each time you do x++, you change the value of x.. // but you "return" the old value of x there?