Pre/Post Increment Operator
Q1:
Ans:
9, 20, 28, 40, 47, 60
Demo:
Explanation:
int* ptr = arr;
*(ptr++) += -1;
++在 ptr 後面, 他會先賦值再移動指標 Step 1. *ptr --> 10 Step 2. *ptr = *ptr + (-1) --> arr[0] = 9 Step 3. ptr++ --> this pointer has shifted to arr[1]
*(++ptr) += -2;
++在 ptr 前面, 他會先移動再賦值 Step 1. ++ptr --> this pointer has shifted to arr[2] Step 2. *(++ptr) == arr[2] = 30 Step 3. *(++ptr) = *(++ptr) + (-2) --> arr[2] = arr[2] -2 --> arr[2] = 28
ptr += 2
ptr += 2 --> this pointer has shifted to arr[4]
*ptr += -3
*ptr += -3 --> arr[4] = arr[4] -3 --> arr[4] = 47
Summary:
Last updated