A c exam raises a series of questions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.