Some difficult things about the "extended Knowledge 3" array
Extended Directory
1. &array+ 1
2. array+1
3. &array[0]+ 1
Questions about &array+, array+ 1 and &array[0]+ 1 are particularly difficult to understand ~-~. So I'll explain it today.
Since the storage unit for each element in the array is continuously allocated, you can access the array as a pointer, which is the first address of the number.
such as: intarray[100];
Array is the first address of the array with values equal to & Array[0]
PS: Through the address, you can quickly and easily access the array of other element values, the method is as follows:
First address + offset
The value of the element I can be A * (Array+i), * (&array[0]+i), array[i]
Array is the first address of the first element of the array , with the &array[0] Same, while &array is to take 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;}
Operation Result:
array[1]= 2
* (array+1) = 2
* (&array[0]+ 1) = 2
[ program 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;}
Operation Result:
array[1]= 2
* (array+1) = 2
* (&array[0]+ 1) = 2293440
Analysis:
Array: Is the first address of the first element of the arrays, that is, the first address of array[0]
&array: The first address of the array is taken.
&array+ 1:
Take the first address of the array, the value of this address plus sizeof (array), which is &array+ 5* sizeof (int), and the first address of the next array .
array+ 1://equivalent to &array[0]+ 1;
is to take the first address of the next element of the array, which is array[1].
:
The address of &array+ 1 was found to be out of bounds, the address of which is array[5], i.e. &array+ 1= &array+ 5* sizeof (int). The address of array+1 and &array[0] is the address of array[1].
"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^
When reproduced, keep the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself
Some difficult things about the "extended Knowledge 3" array