Correlation between pointers and arrays in C language 1
In C language, pointers are a type that stores addresses, such as char * p. Here p Stores the address of character data, int * p, p stores the address of the integer data. We can use * to obtain the content pointed to by this address. Array is a set of data types, such as int arr [] = {,}. Here, an integer array is declared and defined, char arr [] = "hello world"; declares and defines an array of character types. The memory of this array is a string. C Language stipulates that in most cases, the array name is actually the first address of the array element, so we can access the array by using pointers, you can also access the content pointed to by the pointer in the way of array objects. Note that the content pointed to here is not the pointer itself. For example:
Int main () {int I = 0; int arr [10] = {0}; // array initialization int * p = arr; // create a pointer to arr for (I = 0; I <10; I ++) // assign values to the Array Using the pointer principle {* (arr + I) = I ;}for (I = 0; I <10; I ++) // assign the value of {p [I] = I ;}for (I = 0; I <10; I ++) to the space pointed to by the pointer using the array underlying method) // verify the result {printf ("arr [% d] = % d * (p + % d) = % d \ n", I, arr [I], i, * (p + I);} system ("pause"); return 0 ;}
Pay attention to two points in this Code. The first is that multiple pointers can point to the same target, and the second is that when the array uses the pointer principle for access, it has been degraded by itself (C and pointer are mentioned in this book, and it is not very clear to explain what downgrade is). Here we must first make it clear that the pointer is a pointer, not an array, when creating an array, the memory opens up multiple bytes (depending on the size of the creation and the Data Type created), and all pointers only have four bytes, occupying a piece of memory space. The so-called array downgrade means that the array name changes from occupying multiple addresses to the address of the first element of the array. Only two operation-time groups will not be downgraded. When an array is retrieved, such as & arr, or when the sizeof function is called, the Group will not be downgraded. Here is another example:
Int main () {int I = 0; int arr [10] = {0}; // array initialization int * p = arr; // create a pointer to arr printf ("% d", sizeof (arr); printf ("% d", sizeof (p )); printf ("% d", sizeof (arr [0]); printf ("% d", sizeof (& arr); printf ("% d ", sizeof (& arr [0]); printf ("% d", sizeof (* & arr); system ("pause"); return 0 ;}
What is the output result of this Code? Let's analyze it one by one. First, the int type occupies the first output of four bytes. As mentioned above, the sizeof function will not degrade the array, so what we want is the byte of the entire array, which is the second output of 40 and the pointer occupies four bytes. The output result is 4 and the third output is the size of arr [0, it is four bytes. It can also be said that the subscript operation is performed on arr, so that the arr is downgraded or the fourth output. The arr address must be the fifth of the four bytes, it is also the address. What about the last four bytes? Get the address of arr and then describe it. The result is the entire array of arr. The occupied byte is 40 to be continued.