"C Language Learning Notes" array pointer field pointer array

Source: Internet
Author: User

Original address: http://see.xidian.edu.cn/cpp/html/476.html

Memory layout of pointer arrays and arrays of pointers

Beginners always can't tell the difference between a pointer array and an array pointer. In fact, very good understanding:
Array of pointers: first it is an array, the elements of the array are pointers, and how many bytes the array occupies is determined by the array itself. It is the abbreviation for "array of stored pointers".
Array pointer: First, it's a pointer, which points to an array. The 32-bit system will always be 4 bytes, as for how many bytes it points to, I don't know. It is the abbreviation for "pointer to array."

Which of the following is an array pointer, and which is an array of pointers:
A
int *p1[10];
B
int (*P2) [10];
Every time we ask this question in class, there is always something unclear. There is a need to understand the priority problem between symbols.

"[]" has a higher priority than "*". P1 first with "[]" to form the definition of an array, with the array named P1,int * decorated with the contents of the array, that is, each element of the array. So now we know that this is an array that contains 10 pointers to data of type int, that is, an array of pointers. As for P2, it is better to understand that here "()" has a higher priority than "[]", "*" and P2 constitute a pointer to the definition of the pointer variable named P2,int decorated with the contents of the array, that is, each element of the array. The array does not have a name here and is an anonymous array. So now we know that P2 is a pointer to an array of 10 int types of data, the array pointer. We can use the following diagram to deepen our understanding:

Ii. Mandatory conversion of addresses

Let's look at the following example:
struct Test
{
int Num;
Char *pcname;
Short sdate;
Char cha[2];
Short sba[4];
}*p;

Suppose the value of P is 0x100000. The value of the following table expression is how many.
p + 0x1 = 0x___?
(unsigned long) p + 0x1 = 0x___?
(unsigned int*) p + 0x1 = 0x___?
I'm sure there will be a lot of people who don't understand the question at first. In fact, we take a closer look at this knowledge point déjà vu. A pointer variable adds minus to an integer, so how do you parse it?

Remember the difference between our expression "a+1" and "&a+1" in the front. It's the same here, actually. Adding a pointer variable to an integer does not simply add and subtract the integer from the address in the pointer variable. The unit of this integer is not byte but the number of elements. So: the value of P + 0x1 is 0x100000+sizof (Test) *0x1. As for the size of this structure is 20byte, the previous section has been explained in detail. So the value of P +0x1 is: 0x100014.

(unsigned long) p + 0x1 value. This involves casting, casting the value of the pointer variable p to an unsigned long integer. Any value that is cast will change its type. So this expression is actually an unsigned long integer plus another integer. So the value is: 0x100001.

(unsigned int*) p + 0x1 value. The p here is coerced into a pointer to an unsigned integral type. So the value is: 0x100000+sizof (unsigned int) *0x1, equal to 0x100004.

The above problem seems to have no technical content, the following is a technical content: in the x86 system, the value of how much.
Intmain ()
{
int a[4]={1,2,3,4};
int *ptr1= (int *) (&a+1);
int *ptr2= (int) (int) a+1);
printf ("%x,%x", PTR1[-1],*PTR2);
return 0;
}
This is my lecture class A student asks me the question, he sees on the net, is said to have stumped the n individual. After I read the question, I told him that these people certainly do not understand the compilation, a person who understands the compilation, this kind of question is a small case. Here's an analysis of the problem:

According to the above explanation, the difference between &a+1 and a+1 is clear.

PTR1: Cast the &a+1 value to the int* type, and assign to the int* type variable PTR,PTR1 definitely refer to the next int type data for array A. PTR1[-1] is resolved into * (PTR1-1), that is, ptr1 back 4 byte. So the value is 0x4.
PTR2: As explained above, the value of (int) a+1 is the address of the second byte of the element a[0]. This address is then coerced to the value of the int* type to ptr2, meaning that the *PTR2 value should be 4 byte in a row starting with the second byte of the element a[0.

Its memory layout is shown in the following illustration:

Well, here's the question, what's in the 4 consecutive byte. In other words, the value of the element a[0],a[1] is stored in the end. This involves the system's size-end pattern, which is not a problem if you know the assembly. Since you don't know what the current system is, you have to try to test it. The size-end mode and the method of testing the Union keyword is discussed in detail in the first chapter, please go over there and see, it is no longer detailed here. We can use the following function to test the current system pattern.
int Checksystem ()
{
Union check
{
int i;
Char ch;
C
C.I = 1;
Return (c.ch ==1);
}
This function returns 0 if the current system is in big-end mode, and the function returns 1 if it is in small mode. That is, if the return value of this function is 1, the *PTR2 value is 0x2000000. If the return value of this function is 0, the value of *PTR2 is 0x100.



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.