C/c ++ (suspect 3) Relationship between c pointer and array, pointer Array
C/c ++ (Suspect 1) arrays and pointers
C/c ++ (suspect 2) const extern
With the previous two articles, we can introduce the relationship between c/c ++ (suspect 3) c language pointers and arrays in more depth.
1 Overview (relationship between C pointer and array)
A pointer is a pointer. A pointer variable occupies 4 bytes in a 32-bit system, and its value is a memory address. The pointer can point to any place, but not any place you can access it through this pointer variable.
An array is an array. Its size depends on the type and number of elements. When defining an array, you must specify the type and number of its elements. Arrays can store any type of data, but cannot store functions.
2 what is the difference between a and & a (laying the groundwork for the C language pointer array and array pointer below)
Let's 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); // 01 00 00 00 02 00 00 00 03 00 00 04 00 00 00 cc ccint * ptr1 = (int *) (& a + 1 ); int * ptr2 = (int *) (int) a + 1 ); // 00 00 00 02 00 00 00 03 00 00 04 00 00 00 cc ccint * ptr3 = (int *) (int) a + 2 ); // 00 00 02 00 00 00 03 00 00 04 00 00 00 cc 3 cint * ptr4 = (int *) (int) a + 3 ); // 00 02 00 00 00 03 00 00 04 00 00 00 cc 3c d2int * ptr5 = (int *) (int) a + 4 ); // 02 00 00 00 03 00 00 00 04 00 00 cc 3c d2 d8int * ptr = (int *) (& a + 1 ); printf ("% d, % d", * (a + 1), * (ptr-1); printf ("% x, % x, % x, % x ", ptr1 [-1], * ptr2, * ptr3, * ptr4, * ptr5); // the next article explains getchar (); /* a 0x00C4F810 & a [0] 0x00C4F810 & a 0x00C4F810 */return 0 ;}
The above comments are also retained by checking some comments of memory and Assembly. They may be different from each machine, so the addresses are different.Explanation: Add 1 to the pointer to get the address of the next element, instead of adding 1 directly to the original address value. Therefore, the movement of a pointer of type T is measured in sizeof (T. Therefore, for the above question, a is a one-dimensional array with five elements; ptr * is an int pointer.
& A + 1: Get the first address of array a. The value of this address is added with the value of sizeof (a), that is, & a + 4 * sizeof (int ), that is, the first address of the next array. Obviously, the current pointer has crossed the limit of the array.
(Int *) (& a + 1): The address calculated in the previous step is forcibly converted to the int * type and assigned to ptr1.
* (A + 1): the value of a and a is the same, but the meaning is different. a is the first address of the array's first element, that is, the first address of a [0, & a is the first address of the array, and a + 1 is the first address of the next element of the array, that is, the first address of a [1]. & 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 int * type, so * (ptr-1) is pointing to a [3], output 4. Printf ("% x, % x", ptr1 [-1], * ptr2, * ptr3, * ptr4, * ptr5); // the next article (
Pointer array, array pointer) It is relatively complicated and involves memory and large and small terminals. Next time we will introduce it!