C/C + + (suspect 3) c. Relationship between pointers and arrays

Source: Internet
Author: User

C + + (suspect 1) arrays and pointers

C + + (suspect 2) const extern

with the foundation of the previous two articles, we can further introduce c/C + + (suspect 3) c. Relationship between pointers and arrays

1 Overview (C language pointer-to-array relationship)



Pointers are pointers, and pointer variables, under 32-bit systems, always account for 4 bytes, which is the address of one memory. The pointer can point to anywhere, but not anywhere you can access it through this pointer variable.

An array is an array whose size is related to the type and number of elements. When you define an array, you must specify the type and number of its elements. Arrays can store any type of data, but cannot store functions.



2the difference between a and &a(for the followingC-language pointer arrays and arrays pointersbury the foreshadowing)

take a look at the following code
<pre name= "code" class= "CPP" >int _tmain (int argc, _tchar* argv[]) {int A[4]={1,2,3,4};int *ptr0 = (int*) (int) a;//01  At the xx xx xx xx xx, cc cc ccint *ptr1= (int *) (&a+1), int *ptr2= (int *) ((int) a+1),//00 00 00 02 xx xx xx, cc cc cc ccint *PTR3 = (int *) ((int) a+2),//00 xx xx, xx, xx xx CC CC CC 3cint *PTR4 = (int *) ((int) a+3);//00, XX, XX, XX, cc cc CC CC 3c D2int *PTR5 = (int *) ((in t) a+4);//02 (*ptr=);p &a+1 ("rintf", * (%d,%d), * [a+1], * * *, XX, cc cc CC CC 3c D2 d8int ptr-1 (int *) );p rintf ("%x,%x,%x,%x,%x,%x", PTR1[-1],*PTR2,*PTR3,*PTR4,*PTR5);//Next article explains GetChar ();/*a  0x00c4f810&a[0] 0x00c4f810&a 0x00c4f810*/return 0;}


The comments above are some of the comments I've made by looking at the memory and the assembly, which are also preserved, may be different for each machine, so the address is not the same. Explanatory notes: pointer in Row plus 1 operation, the address of the next element is obtained, not the original address value directly plus 1. So, a pointer of type T moves to sizeof (T) for the moving unit. So, for the above question, A is a one-dimensional array with 5 elements in it, and PTR * is a pointer to int.

&a + 1: Take the first address of array A, the value of this address plus the value of sizeof (a), that is, &a + 4*sizeof (int), which is the first address of the next array, It is clear that the current pointer has crossed the bounds of the array.



* (a+1): The value of A,&a is the same, but the meaning is not the same, a is the first address of the first element of the array, that is, the first address of the a[0], &a is the first address of the array, a+1 is the first address of the next element of the array, a[1], and &a+1 is the first address of the next array. So output 2 * (ptr-1): Because PTR is pointing to a[4], and PTR is an int * type, so * (PTR-1) is point to a[3], output 4. printf ("%x,%x,%x,%x,%x,%x", PTR1[-1],*PTR2,*PTR3,*PTR4,*PTR5);//The next article explains ( pointer array, array pointer ) relatively slightly complex, involving memory and size of the end, the next introduction!

C/C + + (suspect 3) c. Relationship between pointers and arrays

Related Article

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.