Array name, pointer, and address, array pointer address
First, let's look at this:
#include <stdio.h>#include <iostream>using namespace std;int main() { int a[2] = {1, 2}; cout << "The address of int_array: " << endl; cout << a << " " << &a << endl << endl; double b[2] = {1.2, 1.3}; cout << "The address of double_array: " << endl; cout << b << " " << &b << endl << endl; char c[2] = {'a', 'b'}; cout << "The address of char_array: " << endl; cout << c << " " << &c << endl << endl; int d[5] = {1, 2, 3, 4, 5}; int dd[5] = {6, 7, 8, 9, 0}; int j = 2, k = 1, i = 3; cout << d[i] << " " << i[d] << endl << endl; //this code is so strange, but in fact, d[i] = *(d + i) = *(i + d) = i[d]. return 0;}
Here we found that a and & a seem to be the same, and there is a char type, why is it different?
First, about a and &:
#include <stdio.h>int main() { int a[5] = {1, 2, 3, 4, 5}; printf("a = %p\n", a); printf("&a = %p\n", &a); printf("a + 1 = %p\n", a + 1); printf("&a + 1 = %p\n", &a + 1); return 0;}
Obviously, a is the address of the first element of the array, and & a refers to the entire array (the length of the two is basically different)
Later I wrote something myself:
# Include <iostream> using namespace std; int main () {int a [5] = {305210130,116 4203589, 2023197048, 4, 5 }; // The first three digits are represented as 12312312, 45645645, and 78978978 in the hexadecimal format. This is mainly used for testing the convenience of int * ptr1 = (int *) (& a + 1); int * ptr2 = (int *) (int) a + 1); for (int I = 0; I <20; I ++) {// 01 cout
Now let's try to explain that the comment sequence numbers in the corresponding code are: 01 and Endian. As a result, the actual data is: (IN hexadecimal notation) 12 31 23 12 45 64 56 45 78 97 89 78 00 00 00 00 00 00 05 the data in the memory should be: (the first line is the label, and the second line is the data, also in hexadecimal format) 0x22ff ---- 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 44 12 23 31 12 45 56 64 45 78 89 97 78 04 00 00 00 05 00 00 00 ?? ?? ?? ?? Another thing to understand is that * (int *) (int) a + I) means (int) a to convert the address of a to the int type (comment 12 ), then (int) a + 1 is the address plus one, this one is the real one, not according to the int size plus four (note 13), (int *) (int) a + I) then converts the int type address to the int address (in hexadecimal format), that is, 0x22ff31 (comment 14), and finally obtains the content. The output result is clear: 0x22ff30: 123123120x22ff31: 451231230x22ff31: 56451231... note that the high zero is omitted: 0x22ff3a: 00047897 --> 47897
02. It indicates the meaning of & a: it is still the address of the array (Special address), but it indicates the length, therefore, the plus one directly adds the length of an array (that is, 20, to hexadecimal format is 14), and the output is 0x22ff30 + 14;
03. Here we will illustrate the particularity of & a in 02, which is equivalent to pointing itself to itself? Therefore, the content is still your own;
04. Obtain the address twice. The first time is the address, and the second time is the content on the address;
05. This is to verify the correctness of 04;
From 06 to 11, the above experiment is correct. Note that the type of ptr1 determines the size of the address offset (-1 is-1 int length );
12 to 15 OK, not difficult;
16. If the ptr2 address is 0x22ff31, * ptr2 is 45123123, And the decimal value is 1158820131;
As mentioned in 17 and 16;
The remaining few are the 01 verification.
Note the following question:
In this way, no problem occurs. & a + 1 refers to the next address of the last bit of the array, while ptr1 [-1] = * (ptr1 + (-1 )), that is, the last digit of the array, with the content of 5. At this time, the array value is: (Note in hexadecimal notation) 00 00 00 01 00 00 02 00... In the memory: (pay attention to the hexadecimal format) 01 00 00 00 00 02 00 00 00 03 00... In this way, the address of a corresponds to the third 00 above, which is converted to int, plus (real one), that is, the next -- 02 of the third 00, convert it to int, that is, 00 00 02, and the actual value (hexadecimal) is 02000000, that is, 2000000.