What is the output of the next code? (This is a written C test question I have done. At that time, 213 was passed and no correct answer was provided. Now let's analyze and summarize it to prevent this 213 error from coming back next time)
Int main (void)
{
Int a [] = {6, 7, 8, 9, 10 };
Int * p =;
* (P ++) + = 123;/* In fact, there is no need to expand p ++. * And ++ are in the same priority, from right to left */
Printf ("% d, % d \ n", * p, * (++ p ));
Return 0;
}
Output: 8, 8
To explain why this is the result, pay attention to the following two points:
1. * (p ++) + = 123; in this question, this line of code actually fooled you, and the simplification is p ++;
2. Pay attention to the transfer of function parameters from the second point. function parameter transfer is implemented through stacks (mostly through stacks and some through registers). Stack operations are performed through FILO, when passing parameters, the first parameter is first added to the stack, then the second parameter, and the third parameter is the opposite when the output stack is used. So why is the first output 8 difficult to understand.
Although code like printf ("% d, % d \ n", * p, * (++ p); is usually despised, I do not advocate such code style and writing style, but as a pen test, it is still a test of basic skills.
This question has been completed, but I will analyze it in depth:
1. * (p ++) + = 123; what is the impact of this line of code?
The value of a [0] is changed to 129;
The p pointer moves one unit backward.
Operations such as p ++ do not affect this statement, but only apply to the next statement.
2. Is a + = B equivalent to a + B?
In most cases, it is equivalent, but there are also special cases. This is what we see below:
* (P ++) + = 123;
Not equivalent
* (P + +) = * (p ++) + 123;
If the first line of code is replaced with the second line, the output result is 9 and 9.
The analysis is here, and the new content is added.
From cjok376240497