A c question

Source: Internet
Author: User

I have always despised the C questions in a row of expressions, ++. Today, I accidentally saw one, but I didn't pay attention to it. I was very lucky when I did it. Alas, it seems that my basic things are still a little weak.

The questions are as follows:

Pointer operation:
Int * P1, * P2;
Int value;

P1 = (int x) 0x500;
P2 = (int x) 0x508;
Value = P2-P1;
Q: What is the value of value?

The answer 2 provided on the Internet is strange for a long time. In fact, value = P2-P1 is equivalent to (508-500)/sizeof (INT ). In this case, the answer is uncertain. On Windows/TC, the value is 4, and on AIX it is 2, the reason is that the size of int in different compilers is different from that in OS.

The question is easy to shake. In fact, it is to let you understand the addition and subtraction operations of the address. For example, the following forms are very common:

Int A [5];
Int I;
Int * P1 =;

For (I = 0; I <5; I ++)
{
* P1 = I;
P1 ++;
}

 

 

 

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.