We may not really understand arrays and pointers at the beginning of a freshman's study of C, but the essence of C is the allocation of pointers and memory.
at best, we can know that arrays and pointers are definitely not exactly equivalent, the same point is: the reference to the array is always converted to a reference to the pointer, and the difference is that the array name is a constant and the pointer is a variable only that, as our seniority continues to improve, we do more to understand it, From his nature to the memory of the allocation and access to understand it. Well, first of all, we have to understand a concept . In C, what is the difference between the declaration and definition of a variable. We know that definitions are just a special declaration. definition : You can only appear in one place, create a new object, determine the type of the object, and allocate memory . (Note that the object here does not have anything to do with the object mentioned in C + +.) ) declaration: can occur more than once. Describes the type of object used to refer to an object defined elsewhere. What it describes is not itself. The extern object declaration tells the compiler the type and name of the object, the memory allocation of the object is elsewhere, and because there is no memory allocated to the array in the declaration, it is not necessary to provide information about the length of the arrays. so let's look at an example:document 1:int mango[100];file 2:exitern int *mango;It always goes wrong when we run, why? First, we're looking at a reference to the memory array and pointers:For example, Char a[9]= "ABCDEFGH"; c=a[i];The compiler symbol table has an address 9980Runtime First step: Take the value of I and add it to 9980run-time step two: Take the value of address (9980+i). but when: Char *a= "ABCDEFGH"; C=a[i];the compiler symbol table has an A with an address of 4624Runtime First step: Take address 4624 content, such as: 5081 (note here: Because P is a pointer, the pointer we know it is the address (that is, the contents of the 4624 cell is still an address), and relative to the subject, its address is the address of the array. )run-time step two: Go to the value of I and add it to 5081. run-time step three: Take the value of the address "5081+1". It is easy to see that the array memory is accessed directly on the face, while the pointer is accessed indirectly. (Although you can eventually get the elements you want, the access path is different!) )when an extern char *a, and then uses A[i] to refer to one of the elements, it is right: char *a= "ABCDEFGH"; C=a[i]; the access steps. so since P is declared as a pointer, whether P was originally defined as a pointer or an array, the element is accessed as a pointer , but only if P was originally defined as a pointer. and then we analyze the beginning. Why is the runtime always wrong ? Mango is declared as extern char *mango, and its original definition is char mango[100], in this case, when extracting the content of this declaration in the form of mango[i], you actually get a character. But according to the above, the compiler is to think of it as a pointer, the ACSII code interpreted as an address, is obviously irrelevant it. The solution is easy: declared as: extern int mango[]; There is the definition of a: char *p= "ABCDEFGH";This is a string constant, and the string constant is defined as read-only. Undefined behavior occurs if you attempt to modify the contents of the string by using the pointer. Contrary to the pointer: an array initialized by a string constant can modify its contents. here is a C programmer's famous saying: Do not forget the C language in the expression of a type T is an array of the left value as a pointer to the first element of the array. when do arrays and pointers have the same? all array names that function as arguments can always be converted to pointers by the compiler. In all other cases (the most interesting scenario is "define an array in one file, declare it as a pointer in another file"), the array declaration is an array, and the pointer declaration is a pointer, and the two cannot be confused. But when you use an array (which is referenced in a statement or expression), the array can always be written as a pointer, and the two are interchangeable. to the compiler, an array is an address, and a pointer is the address of an address. summarize the places and different points of the same point: declaration Aspect : 1, extern such as extern char a[], cannot be written as a pointer form. 2, definition, such as Char a[10]; it should not be written as a pointer form. 3, function parameters, such as fun (char a[]) or fun (char *a) casually write, because when the array as a function parameter, the compilation will convert it to pointer processing. use in expressions : such as c=a[i] can also be casually written, array form or pointer form can be. Standard C stipulates three rules: arrays are the same as pointers 1. The array name in the expression (unlike the Declaration) the white compiler treats as a pointer to the first element of the array. 2, the subscript is always the same as the pointer offset. 3. In the declaration of a function parameter, the array name is used by the compiler as a pointer to the first element of the array. In expressions, pointers and arrays are interchangeable, because their final form in the compiler is a pointer and can be used to remove the subscript. There are, of course, a few rare exceptions: a reference to an array cannot be substituted with a pointer to the first element of the arrays in the following cases:(1), the operand of the array as sizeof () obviously needs the length of the entire array at this time, not a pointer to the first element. (2), use & take the address of the array, it takes an address of the whole array. (3), when the array is a string constant initial value. The root cause of the C language to write the pointer offset is that the pointer and offset are the basic models used by the underlying hardware. The standard specifies that the tragic declaration of "array of types" should be adjusted to "pointer of type". In a special case where the function is undefined, the compiler must rewrite the array form as a pointer to the first element of the array, and the compiler only passes the address of the array to the function, not the copy of the entire array, exponentially. equating arrays and pointers as miserable is a consideration for efficiency reasons. in the C language, all data arguments in non-array form are in the form of a pass (a copy of the argument is passed to the calling function, and the function cannot modify the value of the actual variable that is the argument, but only the copy that is passed to him). However, if you want to copy the entire array, both in time and space, the overhead is great, and in most cases you don't need to copy the entire array, just tell the function that address. Let's look at how the array-shaped tragedy is quoted:Fun (char p[]) or fun (char *p) c=p[i]compiler symbol table display p can be taken, offset from stack pointer sp 14 locationsrun-time step 1: Find the activity record of the function from the SP offset 14 locations, take out the parametersrun-time Step 2: Take the value of I and add to 5081run-time Step 3: Remove the contents of the address (5081+i) Note: There is one operation that can only be done in the pointer and not in the array, which is to modify its value. The array name is a non-modifiable lvalue, and his value cannot be changed. The following three functions are in the same file:
1.FUN1 (int *ptr) 2. {3. ptr[1]=3;4. *ptr=3;5. ptr=array2;//can assign another array name to PTR because it is a pointer 6.} 7.fun2 (int arr[]) 8. {9. arr[1]=3;10. *arr=3;11. Arr=array2;//can also, because the ARR compiler is handled as a pointer to 12.} 13.14.15.int Arrary[100],array2[100];16.main () 17. { array=array2;//compilation error ' cannot modify array name ' 19.}
The similarities and differences between the array and the pointer is over, there are still a lot of content, they are the essence of C, is also a part of the difficult to understand!
Ext.: http://blog.chinaunix.net/uid-26983585-id-3336911.html
Deep understanding of the difference between arrays and pointers