1. Definition and declaration of Distinction
Definition: can only appear in one place. Determines the type of object and allocates memory for creating a new object. e.g. int my_array[100];
Declaration: can appear multiple times. Describes the type of object used to refer to objects defined elsewhere (for example, in other files). For example: extern int my_array[];
2. Pointers and arrays of access mechanisms
Array:
Char a[9] = "ABCDEFGH";
c = A[i];
The compiler symbol table has an address of 9980. (It can be assumed that all A's related operations are replaced with address 9980-related operations when the program is compiled.)
Run-time Step 1: Take the value of I and add it to 9980.
Run-time Step 2: Take the contents of the address (9980+i).
Pointer:
Char *p;
...
c = *p;
The compiler symbol table has a symbol p, which has an address of 4624.
Run-time Step 1: Take the content of address 4624, that is, ' 5081 '.
Run-time Step 2: Take the contents of address 5081.
3. What happens when you define a pointer, but refer to it as an array
Char *p = "ABCDEFGH";
c = P[i];
1. The compiler encountered P[i] This sentence, look up the symbol table, the symbol P is interpreted as a pointer, so first to take the address of the symbol p (for example, 4624) corresponding to the content (such as ' 5081 '), that is, the pointer is stored in p.
2. When the compiler encounters [i], take the value of I and add the value of I to 5801.
3. Take the contents of the address (5081+i).
4. Define an array, but refer to it as a pointer, what happens
Define char a[] = "ABCDEFGH" elsewhere;
extern char *a;
c = a[i];//equals operation C = * (A + i);
1. The compiler encounters symbol A, according to A's declaration, interprets a as a pointer (in fact a is an array). Then follow the steps of the pointer, first to take the address of the symbol a corresponding content, but the address of a is a character, such as ' a ' B ' C ' d ' (here the pointer as 32-bit).
2. When the compiler encounters [i], take the value of I and add the value of I to the binary value of ' a ' B ' C ' d '.
3. It is obviously wrong to take the value of the above address.
C Expert programming note (iv)--arrays and pointers are not the same