post-increment

Is ++ the same as += 1 for pointers?

谁都会走 提交于 2019-12-10 13:22:26
问题 I'd like to refactor some old C code of mine, and I was curious if I can replace all ptr++ with ptr += 1 where ptr is some pointer, without changing any behavior. Here's an example of what I mean, from K&R Section 5.3: /* strlen: return length of string s*/ int strlen(char *s) { int n; for (n = 0; *s != '\0'; s++) n++; return n; } When I replace the s++ with s += 1 , I get the same results, but I'm wondering if this will be the case for all types. I also did a test for int s: int size = 10;

Pre increment and post increment

梦想与她 提交于 2019-12-10 10:35:23
问题 I'm having trouble understanding how Post Increment ( ++ ), Pre Increment ( -- ) and addition/subtraction work together in an example. x++ means add 1 to the variable. x-- means subtract 1 from the variable. But I am confused with this example: int x = 2, y = 3, z = 1;` y++ + z-- + x++; I assume this means 3(+1) + 1(-1) + 2(+1) Which means the result should be 7. But when I compile it, I get 6 . I don't understand. int main() { int x=2, y=3, z=1; int result; result = y++ + z-- + x++; //this

Puzzling behaviour of == after postincrementation [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-10 02:49:58
问题 This question already has answers here : Integer wrapper objects share the same instances only within the value 127? [duplicate] (5 answers) Closed 6 years ago . Someone postulated in some forum thread that many people and even experienced Java Developers wouldn't understand the following peace of Java Code. Integer i1 = 127; Integer i2 = 127; System.out.println(i1++ == i2++); System.out.println(i1 == i2); As a person with some interest in Java I gave it my thoughts and came to the following

i++ vs. ++i in a JavaScript for loop

我与影子孤独终老i 提交于 2019-12-09 17:10:22
问题 Because of JSLint, I almost always use i += 1 to increment a JavaScript for loop, but for quick and dirty scripts, I use i++ instead. However, I see a lot of for loops in other people's code in which they increment i by doing ++i instead. As far as I know, there is no difference in meaning between i++ and ++i , and jsPref shows no difference in performance. As such, I'm wondering where the convention of doing ++i comes from and why people tend to do it. Does anyone know why a lot of JS coders

Understanding more about i++ and i=i+1

本小妞迷上赌 提交于 2019-12-09 10:06:45
问题 I was wondering if there is difference between the two forms of increment. Some of the links says i++ is faster that i=i+1; Also as one of the person my observation is also the same for assembly code. please check the image where both the assembly code are same for i++ and i=i+1 - There is another link that says that it used to be true previously that increment operator was faster than addition and assignment, but now compilers optimize i++ and i=i+1 the same. Is there any official document

Is the behavior of return x++; defined?

旧城冷巷雨未停 提交于 2019-12-09 08:23:37
问题 If I have for example a class with instance method and variables class Foo { ... int x; int bar() { return x++; } }; Is the behavior of returning a post-incremented variable defined? 回答1: Yes, it's equivalent to: int bar() { int temp = x; ++x; return temp; } 回答2: Yes it is ... it will return the x's value before incrementing it and after that the value of x will be + 1 ... if it matters. 回答3: Yes. In postincrement (x++) the value of x is evaluated (returned in your case) before 1 is added. In

Semantics of pre- and postfix “++” operator in Java [duplicate]

冷暖自知 提交于 2019-12-09 03:43:39
问题 This question already has answers here : Java: Prefix/postfix of increment/decrement operators? (10 answers) Closed 6 years ago . I wondering to know why this snippet of code give output 112 How this last digit 2 was creating? public static void main(String[] args) { int i = 0; System.out.print(++i); System.out.print(i++); System.out.print(i); Why does this happen? 回答1: Your snippet it's translated as int i = 0; i = i + 1; // 1 System.out.print(i); // 1 System.out.print(i); // 1 i = i + 1; //

Why isn't mySet.erase(it++) undefined behavior, or is it?

别说谁变了你拦得住时间么 提交于 2019-12-08 15:39:05
问题 Accordint to this quite highly upvoted answer, the canonical way to iterate through a set erasing some elements is the following: for (it = mySet.begin(); it != mySet.end(); ) { if (conditionToDelete(*it)) { mySet.erase(it++); } else { ++it; } } This, of course, is a result of C++03's set erase not returning an iterator. Otherwise one could write it = mySet.erase(it); It is also obvious that one can write itToDelete = it++; mySet.erase(itToDelete); This question is not about how to delete

post increment behaviour [duplicate]

大憨熊 提交于 2019-12-07 16:23:33
问题 This question already has answers here : What is x after “x = x++”? (17 answers) Closed 6 years ago . i have small doubt.why the below code is printing value i=2. int i=2; i=i++; System.out.println(i); can someone please explain me what is happening in line no 2. so there is no meaning here of doing ++ here? Thanks 回答1: i=i++; Because first the assignment happens, then the increment applies. Something like: first i gets 2, then ++ operation happens, but results won't be re-assigned to i, so i

Behaviour of PreIncrement and PostIncrement operator in C and Java [duplicate]

℡╲_俬逩灬. 提交于 2019-12-07 02:37:04
问题 This question already has answers here : Why are these constructs using pre and post-increment undefined behavior? (14 answers) Closed 6 years ago . I'm running the following programs in Visual C++ and Java: Visual C++ void main() { int i = 1, j; j = i++ + i++ + ++i; printf("%d\n",j); } Output: 6 Java: public class Increment { public static void main(String[] args) { int i = 1, j; j = i++ + i++ + ++i; System.out.println(j); } } Output: 7 Why the output in these two languages are different?