Question2

What will be the output of the following C code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main() {
int arr[] = {6, 7, 8, 9, 10};
int *ptr = arr;

*(ptr++) += 123;

printf("%d\n", *(ptr - 1));
printf("%d\n", *ptr);
printf("%d\n", *ptr, *(++ptr));

getchar();

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
分析:
*(ptr++) += 123; => *ptr = *ptr + 123; ptr++;

所以ptr 指向arr 的第二個成員,且arr 的值變為{129, 7, 8, 9, 10}

依序為:
*(ptr - 1) => 129
*ptr => 7

printf("%d\n", *ptr, *(++ptr)),則先看*(++ptr),再看*ptr

*(++ptr) => 8
*ptr => 8