Questions tagged [post-increment]

For issues relating to defining or performing post increment operations.

Post-increment operators increase the value of their operand by 1 (or another specified amount), but the value of the operand in the expression is the operand's original value prior to the increment operation.

561 questions
3
votes
1 answer

Postfix Operator Overloading Order

Possible Duplicate: Undefined Behavior and Sequence Points I'm having trouble understanding the order of actions when overloading the postfix operator. Let's examine the two small examples below: int i = 0; std::cout << std::endl << "i: " <<…
Walker
  • 1,215
  • 2
  • 13
  • 26
3
votes
2 answers

In Java, i++ is unary operator or arithmetic operator?

I have searched the web and it gives me a divide answer. Some said it is only an unary operator and some said it is an arithmetic operator. Thanks
Ko Ye
  • 77
  • 2
3
votes
4 answers

Can the post-increment operator be used in the parameters of a function call? in C?

My question pertains to function calls in general, but I thought of it while I was writing a priority queue using a heap. Just to give some context (not that it matters much) my heap stores items top to bottom left to right and I represent the heap…
user11740831
3
votes
4 answers

When does postincrement i++ get executed?

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…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
3
votes
1 answer

C++ infinite loop when assigning and post incrementing the loop iterator (gcc bug?)

I came across a strange typo in one of our C++ applications the other day that triggered the code to enter an infinite loop. At the end of a for loop, instead of just incrementing i++, the programmer accidentally assigned i = i++. for (int i = 0; i…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
3
votes
4 answers

switch statement and incrementation operator

I wrote the following code: int i = 0; switch(i++) { case 0: cout << 0; case 1: cout << 1; } cout << "\n" << i; The output of the code was like this: 01 1 Can anyone please explain the first line of output? Why are…
AvinashK
  • 3,309
  • 8
  • 43
  • 94
3
votes
3 answers

When dereferencing and post-incrementing a pointer to function pointer, what happens first?

Given this code: typedef void (*Thunk)(); Thunk* gFP; void foo(){ printf("Foo "); *gFP(); }; void bar(){ printf("Bar "); Thunk Codex[] = { foo, bar }; gFP = Codex; (*gFP++)(); Does the function call happen before or after the…
AShelly
  • 34,686
  • 15
  • 91
  • 152
3
votes
5 answers

How does this code work?

I am looking at c++ for dummies and found this code #include #include #include using namespace std; int nextStudentId = 1000; // first legal Student ID class StudentId { public: StudentId() { value =…
user451498
3
votes
3 answers

c++ spaces in operators , what are the rules

Does spaces have any meaning in these expressions: assume: int a = 1; int b = 2; 1) int c = a++ +b; Or, 2) int c = a+ ++b; When I run these two in visual studio, I get different results. Is that the correct behavior, and what does the…
user3599803
  • 6,435
  • 17
  • 69
  • 130
3
votes
1 answer

Result of double postincrementation - in if statement and its body

Could someone explain me why this short line of code is returning 1? int i = 0; if(i++) i++; printf("%d", i); I mean when checking the if statement i has to be incremented otherwise the result would not be 1. But then as it is incremented is should…
Lisek
  • 753
  • 2
  • 11
  • 31
3
votes
1 answer

Postfix vs. prefix increment with lambda expressions

This question is definitely not a duplicate because I do not refer to general post/precrement evaluation but specifically to the use of increment in lambda expressions, which is provably different. Everyone is free to confirm that the code will…
AdHominem
  • 1,204
  • 3
  • 13
  • 32
3
votes
3 answers

Pre/Post Increment Pointers in C++

*(p1++) int array[10] = {1,2}; int *p1 = array; *p1=24; *p1= *(p1++); for (int i : array) cout << i << " "; Output is 24 24 *(++p1) int array[10] = {1,2}; int *p1 = array; *p1=24; *p1= *(++p1); for (int i : array) cout << i << " "; Output…
Patrick Duncan
  • 549
  • 1
  • 6
  • 17
3
votes
3 answers

Order of incrementing and dereferencing pointer in C++

I tutor students in C++, and recently came across a problem involving pointer arithmetic with array names. The main thing I'm confused about is the statement T min_value = *begin++; Cplusplus tells me that the ++ operator has higher precedence…
wyverniv
  • 279
  • 1
  • 2
  • 11
3
votes
2 answers

Why is "ref" not assigning updated value to the next field?

I've found a really cool feature of a compilator. However, I cannot understand logic of this behaviour. static int IncrementValue(ref int i) { return i++;} and Main method: static void Main(string[] args) { int a = 2; int b =…
StepUp
  • 36,391
  • 15
  • 88
  • 148
3
votes
3 answers

Different Output for b=+1 in java

I executed the below program int b = 0; b=+1; System.out.println(b); b=+1; System.out.println(b); b=+1; System.out.println(b); and got output like 1 always. Why is the value of b incrementing in the first increment and…
robin
  • 1,893
  • 1
  • 18
  • 38