post-increment

Is there a difference between ++i and i++ in this loop?

你。 提交于 2019-11-30 09:03:47
问题 The array.prototype.reduce function at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce It has the following loop: for (index = 0; length > index; ++index) { if (this.hasOwnProperty(index)) { if (isValueSet) { value = callback(value, this[index], index, this); } else { value = this[index]; isValueSet = true; } } } I don't think there is a difference whether the index is pre or post incremented here since it's done after the loop iterates each time

Why is this Java operator precedence being ignored here?

纵然是瞬间 提交于 2019-11-30 05:58:01
问题 The following code prints out "3", not "4" as you might expect. public class Foo2 { public static void main(String[] args) { int a=1, b=2; a = b + a++; System.out.println(a); } } I understand how. The postfix increment happens after the value of "a" has been loaded. (See below). What I don't quite understand is the why. The operator precedence of postfix ++ is higher than + so shouldn't it execute first? % javap -c Foo2 Compiled from "Foo2.java" public class Foo2 extends java.lang.Object{

Difference between *ptr += 1 and *ptr++ in C

巧了我就是萌 提交于 2019-11-29 21:09:15
I just started to study C, and when doing one example about passing pointer to pointer as a function's parameter, I found a problem. This is my sample code : #include <stdio.h> #include <string.h> #include <stdlib.h> int* allocateIntArray(int* ptr, int size){ if (ptr != NULL){ for (int i = 0; i < size; i++){ ptr[i] = i; } } return ptr; } void increasePointer(int** ptr){ if (ptr != NULL){ *ptr += 1; /* <----------------------------- This is line 16 */ } } int main() { int* p1 = (int*)malloc(sizeof(int)* 10); allocateIntArray(p1, 10); for (int i = 0; i < 10; i++){ printf("%d\n", p1[i]); }

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

青春壹個敷衍的年華 提交于 2019-11-29 17:22:12
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? The loop is equivalent to: int x = 2; { int y = 2; while (y > 0) { System.out.println(x + " "+ y + " "); x++; y--; // or --y; } } As you can see from reading that code, it doesn't matter whether you use the post or

Multiple increment operators in single statement [duplicate]

情到浓时终转凉″ 提交于 2019-11-29 16:27:28
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? Greg Howell 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: §5.2.2/8 The order of evaluation of arguments is unspecified. The standard says this is

What should be the output of echo ++$a + $a++ [duplicate]

无人久伴 提交于 2019-11-29 14:45:05
问题 This question already has an answer here: Why is $a + ++$a == 2? 13 answers In the PHP manual, operator precedence section, there is this example: // mixing ++ and + produces undefined behavior $a = 1; echo ++$a + $a++; // may print 4 or 5 I understand the behavior is undefined because of the following reason: Since x + y = y + x the interpreter is free to evaluate x and y for addition in any order in order to optimize speed and/or memory. I concluded this after looking at the C code example

Post increment and Pre increment in C

♀尐吖头ヾ 提交于 2019-11-29 12:58:17
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 precedence in these situations. You're mistaken about the meaning of your 2] . Post-increment always

Is there a difference between ++i and i++ in this loop?

倖福魔咒の 提交于 2019-11-29 11:37:50
The array.prototype.reduce function at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce It has the following loop: for (index = 0; length > index; ++index) { if (this.hasOwnProperty(index)) { if (isValueSet) { value = callback(value, this[index], index, this); } else { value = this[index]; isValueSet = true; } } } I don't think there is a difference whether the index is pre or post incremented here since it's done after the loop iterates each time, but want to be certain. Can this be changed to index += 1 so it passes jslint? Please don't debate

increment value of int being pointed to by pointer

做~自己de王妃 提交于 2019-11-28 20:00:22
I have an int pointer (i.e., int *count ) that I want to increment the integer being pointed at by using the ++ operator. I thought I would call: *count++; However, I am getting a build warning "expression result unused" . I can: call *count += 1; But, I would like to know how to use the ++ operator as well. Any ideas? Doug T. The ++ has equal precedence with the * and the associativity is right-to-left . See here. It's made even more complex because even though the ++ will be associated with the pointer the increment is applied after the statement's evaluation. The order things happen is:

Difference between *ptr += 1 and *ptr++ in C

江枫思渺然 提交于 2019-11-28 17:16:55
问题 I just started to study C, and when doing one example about passing pointer to pointer as a function's parameter, I found a problem. This is my sample code : #include <stdio.h> #include <string.h> #include <stdlib.h> int* allocateIntArray(int* ptr, int size){ if (ptr != NULL){ for (int i = 0; i < size; i++){ ptr[i] = i; } } return ptr; } void increasePointer(int** ptr){ if (ptr != NULL){ *ptr += 1; /* <----------------------------- This is line 16 */ } } int main() { int* p1 = (int*)malloc