A classic pointer

Source: Internet
Author: User

1.CodeAs follows:

# Include <stdio. h> Int  Main (){  Int A [ 5 ] = { 1 , 2 , 3 , 4 , 5  };  Int * Ptr1 = ( Int *) (& A + 1 );  Int * Ptr2 = ( Int *)(( Int ) A + 1  );  Int * Ptr3 = ( Int *) (A + 1  ); Printf (  "  % X, % x, % x \ n  " , Ptr1 [- 1 ], * Ptr2 ,* Ptr3 ); Return   0  ;} 

The output is "5, 2000000, 2"
What will be output if printf ("% x \ n", * ptr1?

2. Post the code in memory

What is the value of P + 1 for a pointer? This 1 should be sizeof (* P), that is, the size of the structure pointed to by P, so & A + 1 = & A + sizeof (A) = 0x28ff04, A + 1 = a + sizeof (* A) = 0x28fef4, (INT) A + 1 = a + 1 = 0x28fef1;
Ptr1 [-1] = * (ptr1-1) = * (ptr1-sizeof (INT) = * (0x28ff00) = 5;
* Ptr2 = * (0x28fef1) = (memory small-end storage) 00 00 00 02 = 0x2000000;
* Ptr3 = * (0x28fef4) = 2;

* Ptr1 = * (0x28ff04) = 0x28ff04; this is not a coincidence, because ptr1 is defined after array A and followed by array a in memory.

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.