[Extended knowledge 3] array problems and array expansion difficulties
[Extended knowledge 3] array difficulties
Extended directory
1. & array + 1
2. array + 1
3. & array [0] + 1
The problems related to & array +, array + 1, and & array [0] + 1 are particularly difficult to understand ~ -~. So I will explain it today.
Because the storage units of each element in the array are continuously allocated, you can use pointers to access the array. The array name is the first address of the number.
For example, intarray [100];
Array is the first address of the array, and its value is equal to & array [0]
PS: Through the Receiving address, you can quickly and conveniently access other element values in the array, as follows:
First address + offset
The value of element I can be * (array + I), * (& array [0] + I), array [I]
Array is the first address of the first element of the array, which is the same as & array [0], and & array is the first address of the array.
[Program 1]
#include <stdio.h> int main( void ){ int array[ 5 ]= { 1, 2, 3, 4, 5 }; printf( " array[ 1 ]= %d\n", array[ 1 ] ); printf( " *(array+1)= %d\n", *(array+ 1) ); printf( "*( &array[ 0 ]+ 1 )= %d\n", *(&array[ 0 ]+ 1 ) ); return 0;}
Running result:
Array [1] = 2
* (Array + 1) = 2
* (& Array [0] + 1) = 2
[Procedure 2]
# Include <stdio. h> int main (void) {int array [5] = {1, 2, 3, 4, 5}; printf ("array [1] = % d \ n ", array [1]); printf ("* (array + 1) = % d \ n", * (array + 1); printf ("* (& array + 1) = % d \ n ", * (& array + 1); // cross-border !!! Return 0 ;}
Running result:
Array [1] = 2
* (Array + 1) = 2
* (& Array [0] + 1) = 2293440
Analysis:
Array: the first address of the array's first element, that is, the first address of array [0 ].
& Array: the first address of the array.
& Array + 1:
Take the first address of the array. The value of this address is added with sizeof (array), that is, & array + 5 * sizeof (int), which is also the first address of the next array.
Array + 1: // equivalent to & array [0] + 1;
Is the first address of the next element of the array, that is, the first address of array [1.
:
It is found that the address of & array + 1 is out of bounds. It is the address of array [5], that is, & array + 1 = & array + 5 * sizeof (int ). The addresses of array + 1 and & array [0] are the addresses of array [1.
[Smile at your fingertips] errors are inevitable. I hope you can get your corrections ^-^
Reprinted when retaining the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself