Int a [5] = {1, 2, 3, 4, 5}; printf (& quot; % d \ n & quot;, * (int *) (& amp; a + 1)-2);, printf % d

Source: Internet
Author: User

Int a [5] = {1, 2, 3, 4, 5}; printf ("% d \ n", * (int *) (& a + 1)-2 );, printf % d

What is the result of a certain convincing pen question in a certain year? The answer is 4. Why?

My understanding (do not know if it is correct ):

& A is an array pointer of the int type [5], so & a plus 1 is actually a + sizeof (int) * 5, that is, a [5], forced conversion to int type is a + 5, and the result is a [3] = 4.



# Include <stdioh> void main () {int a [5] = {1, 2, 4, 5}; int * ptr = (int *)(&

The key to this question is int * ptr = (int *) (& a + 1); In this sentence, & a indicates obtaining the first address of the storage area of array, add 1 to indicate the address of the storage area of array a, which causes the ptr pointer to point to the address of the storage unit behind the last element of the array. After ptr minus 1, for data access, the value of the previous storage unit of the ptr pointer is accessed. All the final answers are 2, 5.

Int a [] = {1, 2, 4, 5}; int * p = a; printf ("% d \ n", * p); why is the result 1?

The output sequence is run from right to left, and the last is * P. Because * p = a, * P outputs the first element of the array.
* P = 1;
-- (* P) First Auto minus = 0, * p = 0;
* P -- first copy = 0;
* P = 0;
(* P) ++ first copy = 0;
* In (++ p), ++ p automatically adds 1 to the element whose coordinates are 1. a [1] = * (++ p) = 2;
* P also points to a [1], * p = 2;

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.