The following is transferred from: http://bbs.csdn.net/topics/390733584
The feeling is useful to oneself, the collection. If reprint, please specify the source as above. Thanks a lot!
See an article on the C language pointer, just recently looked at "C and the pointer" went to see it. "A C-language pointer to the extension of the topic," the first topic in the puzzle I was stumped.
Help 123456789 #include <stdio.h> int main () {int a[5] = {1, 2, 3, 4, 5}; int *PA = (int) (&a) + 1; printf ("%x\n", *PA); return 0; }
The correct output should be 2000000. Self-styled to the C pointer also more understanding, is not to save the memory address of the variable? What is difficult, later found that I was wrong. I think is &a, is an array name to address, the result is not to save the address of a, so that he converted to int variable and then add 1, and then assign to a int* pointer is error, the result I compiled a bit, really wrong:
Error:invalid conversion from ' int ' to ' int* '.
The int value cannot be assigned to the INT * pointer variable, I modified it, in (int) (&A) + 1, preceded by a coercion type conversion (int *), the output after compiling:
I thought it was right, though he said the correct answer was 2000000. Then I took a closer look at the source code, found int *pa = (int *) (int) (&a) + 1; This code first converts the &a into a pointer and then adds 1 to its base. The original meaning is to give &a plus 1 and then converted to a pointer, the result I modified the code: INT *PA = (int *) ((int) (&A) + 1); So the result is correct, output 2000000:
That's when I'm totally in a daze, what's going on? Later I output &a and a as integers, to see what it was, and found
Help printf ("%d\n", &a); printf ("%d\n", a);
Their output is the same, that is, the value of a and &a, I think the problem is solved here, but I still do not understand why the output 2000000. So I went to the Internet to search the information, the results of the log group name to find the address is what? Inside said
“
Help 123 1 int array[100]; 3 memset (array, 0, sizeof (array)); 4 memset (&array, 0, sizeof (array));
What's the difference between line 3rd and line 4th? In fact, the effect is the same, but it is important to note that the type of array and &array is different. Array is equivalent to &array[0], and &array is a pointer to int[100], and the type is int (*) [100]. ”
Finally know the difference between a and &a, but I still do not understand why it will output 2000000, this is why?
I tried to change the code:
int a[] = {1,5};
This results in the following output: 5000000 original and the second element of the array A is related. Haha, a little understand, after i n times the test.
First of all, our code is:
Help 123 int a[] = {n}; int *PA = (int *) ((int) (&AMP;A) + 1); printf ("%x\n", * (PA));
The result of my compilation a[0] is that the address of a for 0x22ff70 a[1] is 0x22ff74. I looked at the memory (using printf), and the data in memory was like this:
Help 0x22ff70 0x22ff71 ... 0x22ff74 ... 0x22ff77 01 00 ... 02 ... 00
And we know that accessing the in-memory data is a high-byte address and the low-byte address is low. That is, if my address is 0x22ff70, to this address to access an integer, the resulting integer is the 73,72,71,70 data arrangement that is 00000001 (previously learned a little assembly, coupled with their own experiment, out of:), that is, 1. If access to 0x22ff74 is 00000002 is 2. If you access 0x22ff71, that is, starting from 74 02000000. That's not the right answer for us. 2000000.
Problem Solving! It's a bit messy, don't blame it. If there is any mistake, please correct me!
"Go" C-language array names and pointers