C + + (suspect 4) pointer arrays and array pointers relationships

Source: Internet
Author: User

1 pointer arrays and array pointers Overview (array pointers (pointers to arrays) and arrays of pointers (arrays holding pointers))


Array of pointers: first it is an array, the elements of the array are pointers, and the number of bytes in the array is determined by the array itself. It is the abbreviation for "array of stored pointers".

Array pointer: First it is a pointer to an array. The 32-bit system will always be 4 bytes, as to how many bytes it points to, not known. It is the abbreviation for "pointers to arrays".


Understanding pointer arrays and arrays of pointers from memory aspects
1 int *ptr1[5];

2 int (*PTR2) [5];
to analyze the above questions, I want you to know the precedence between the symbols (C language Getting Started tutorialIt will also analyze these basic, but practical, knowledge. This place directly explains:[] "priority is higher than" * "
*****ok with the above foundation, you can start analyzing .
PTR1 is first combined 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. Now we know that this is an array that contains 5 pointers to data of type int, that is, an array of pointers. As for Ptr2 , it is better understood that the precedence of "()" Here is higher than "[]", "*" and P2 constitute a pointer definition, and the pointer variable named P2,int modifies 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 that contains 5 data of type int, that is, an array pointer.


Continue to use memory to analyze






2 continue to see the relationship between addresses

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; }

PTR1: Cast the value of &a+1 to the int* type, the variable ptr,ptr1 assigned to the int* type will definitely refer to the next int type data of array A. PTR1[-1] is parsed into a * (ptr1-1), that is, ptr1 backwards 4 byte. So its 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]. The address is then coerced to the value of the int* type to ptr2, meaning that the value of *ptr2 should be 4 bytes in a row starting with the second byte of the element a[0].

Its memory layout is as follows:
Okay, here's the question, what's in the 4 bytes in a row? This means that the value of the element a[0],a[1] is stored in the end. This involves the system's size and end mode, which is not a problem if the assembly is understood. Since you do not know what the current system is, you have to try to test it. The size-and-end mode and test methods have been discussed in detail in the first chapter on the Union keyword, please turn to the other side to see, here is no longer detailed. We can use this function to test the mode of the current system.
int Checksystem () {    Union check    {       int i;      char ch;    } C;    C.I. = 1;    Return (c.ch ==1); If the current system is in big-endian mode, this function returns 0, or if it is a small-end mode, the function returns 1. This means that if the return value of this function is 1, the value of *PTR2 is 0x2000000. If the return value of this function is 0, the value of *PTR2 is 0x100




C + + (suspect 4) pointer arrays and array pointers relationships

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.