A murder test caused by the murder of int *ptr2= (int *) ((int) a+1);

Source: Internet
Author: User

A day, see a more disgusting C language test, test a lot of comparative knowledge points, face as follows:

int main (void)

{

int a[4] = {1, 2, 3, 4};

int *ptr1= (int *) (&a+1);

int *ptr2= (int *) ((int) a+1);

printf ("%x,%x/n", Ptr1[-1], *PTR2);

return 0;

}

Q, what is output under the x86 platform?

Although the topic is disgusting, but as an example to analyze, it is quite fun. Learn C language friends can not look at the following, I try to analyze, to see if the results are the same as mine, it is also an interesting thing.

The following is a question about all the edges and corners of the problem to comb over, detailed discussion as follows:

1,&a+1

First, it is clear that a is the name of an array with 4 integer variables, specifically the first address of the first element of the array, and &a is the first address of the array, please note the wording. For pointers plus 1, you need the knowledge of pointer arithmetic. Without learning pointer arithmetic or having forgotten this point of knowledge, here are the facts you need to know about pointer arithmetic:

As in the example above, the &a+1 represents a pointer addition, not an ordinary numeric addition, because &a is a pointer rather than a normal value (though it is essentially an integer). Then you will ask: Join at this time &a=0xffff5700, then &a+1 is how much? the answer is: depending on the type of &a .

A) If &a is a pointer to a char type, then &a+1 = 0xffff5701

b) If &a is a pointer to the short type, then &a+1 = 0xffff5702

c) If &a is a pointer to the int type, then &a+1 = 0xffff5704 (32-bit machine)

d) &a+1 = 0xffff5700+sizeof (struct foo) if the &a is a pointer to a struct-type Foo

......

Haven't you seen the clue? Yes, the pointer plus 1 is not simply add 1 to the pointer content, but the pointer to the next data , plus 2 is the pointer to the next two data, the type of the data is the pointer to the type, so the pointer to the addition of exactly where the pointer points to, depending on the data type that the pointer points to.

So, to sum up, when &a and integer 1 Do addition, is actually the addition of pointers, plus 1 means: so that pointer A to the next data , what is the next data? Of course it's next to an array with 4 integer variables (because the type of &a is a pointer to an array with 4 integer variables), and a points to the next address of 4, where the PTR1 is initialized with this value, so ptr1 points to:

Since the PTR1 is initialized, the &a+1 is cast to an integer pointer (see http://blog.csdn.net/seton040/archive/2009/10/19/4699869.aspx for a detailed discussion of type conversions). So ptr1[-1] is equivalent to moving ptr1 forward an integer size, which is 4 bytes. Such as:

Obviously, the first number printed is the content of a[3], which is the value 4.

Of course, we must also explain the fact that the array subscript can be negative, in fact, take off the internal implementation of the symbol "[]" is the pointer operation! For example a[2], equivalent to * (A+2), that is, a address is a base address, take the value of an offset of 2 addresses. So ptr1[-1] is equivalent to * (PTR-1).

2,(int *) ((int) a+1)

As for the treatment of the pointer ptr2 a little more disgusting, hehe! It first casts the array name a into an integer variable, then adds 1 and then casts it into an integer pointer! Really wordy, but it does not matter, we have patience, is nothing more than let PTR2 point to A[0] the second byte, this time PTR2 point as shown:

Obviously, the content printed at this point is the next 4 bytes of content pointed to by PTR2, that is, the first byte of the three bytes and a[1] of a{0}, what exactly will it print? The above figure does not draw the contents of the inside, if the content of each byte is drawn out, hehe!

To print the contents of each byte is not a problem, but you first need to know the concept of byte order, byte order of two kinds, one called Big endian byte order (Big-endian) , of course, in addition to the small end byte sequence ( Little-endian) , let's use a problem to elicit the concept of byte-order, and then take care of the two kids!

The problem is this: how does a machine interpret a variable that is stored in multiple bytes?? Please see:

If this is a normal int variable, the earth knows that on the 32 machine, int takes 4 bytes to store data, as shown in, put a bunch of numbers in 4 bytes, but will the machine actually interpret this number as 0x0103070f or explain it to 0x0f070301? The answer is: it's all possible!

Official explanation:

1, the so-called big-endian (Big-endian) sequence, is high-priority position corresponding to the high-effective bit .

2, the so-called Small end (Little-endian) sequence, is the high-priority position corresponding to the low-effective bit .

Folk explanations:

1, the so-called big-endian (Big-endian) sequence, is to read or store data, the lowest bit corresponding to the high address .

2, the so-called Small end (Big-endian) sequence, is to read or store data, the lowest bit corresponding to the low address .

For example, to put the 0x0103070f into memory, if the 0E is placed in the high address is a small end sequence, if the 0F into the low address is the big-endian sequence. According to this, the data stored in the machine is understood to be 0x0103070f is the machine is the big-endian, otherwise if understood as 0x0f070301 is the small end sequence.

Back to the original question, this time ptr2 points to the second byte of A[0]. We take the x86 platform as an example (small-endian), at which point the internal data distribution is this:

Since the x86 platform is small-endian, according to the theory we have just discussed, small-endian access to the lowest bit corresponding to the low address , so will be printed 0200 0000, if the topic is not described in the x86 platform, the answer is uncertain, depending on the specific platform, ARM platforms, for example, are big endian.

To this question is basically a satisfactory solution, but I also want to wordy, about the code in the A, and that &a, it is estimated that there are many friends in the heart is not very smooth, every encounter array ah pointer ah, although the program seems to seem to have nothing wrong, but the total feeling is mixed past, This time we are going to solve it completely. Remember someone who said that a real programmer has to be familiar with every detail of the code he writes.

Consider this definition again:

int a[4] = {1, 2, 3, 4};

At this point we must make it clear that the compiler assigns us the appropriate size of the storage area according to the type and array size we provide, and that this storage area is called a, note: &a, as we mentioned above, its type is "pointer to an array of 4 integer variables", in short & A is an array pointer. This array of name a, when it is the right value (in simple terms, appears on the left of the assignment number "=" is the left value, which appears on the right of the assignment number "=" is the right value) represents the first address of the first element of the array, not the first address of the array. At this point the meaning of a is equivalent to &a[0], which is a pointer to the first element. Remember!

The name of the array will be equivalent to the pointer in another place, that is, the function parameter table, when it appears in the function of the parameter table, whether you write an array, or pointers, once inside the function, all become pointers.

A murder test caused by the murder of int *ptr2= (int *) ((int) a+1);

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.