In C, sometimes pointers are equivalent to arrays, but sometimes they are not.
When is it different?
1. the array name itself represents an address. The pointer represents the address of the address. Therefore, the definition of pointers and arrays must strictly match the declaration of multiple possible locations! The array defined in one place cannot be declared as a pointer elsewhere. That's not the case! (For details, see Expert C p84 .)
2. pointers and arrays can both be initialized with string constants in their definitions. Although it looks the same, the underlying implementation mechanisms are different. When defining a pointer, the compiler does not allocate space for the object to which the Pointer Points. It only allocates space for the pointer. Unless a String constant is assigned to the pointer during initialization. For example, char * P = "breadrain". Note that this is only true for string constants. Do not expect to allocate space for constants such as floating point numbers: Float * Pip = 3.14. This statement is incorrect, compilation fails. (In ansi c, the String constant created during pointer Initialization is defined as read-only. If you try to modify this string through a pointerProgram). The array can also be initialized with a String constant and can be modified: Char A [] = "breadrain"; character pointer and character array, which can be printed directly by the pointer name and array name.
When is the same?
1. the array name in the expression is used by the compiler as a pointer to the first element of the array.
2. In the declaration of function parameters, the array name is treated as a pointer to the first element of the array by the compiler.
You must remember that pointers and arrays can be exchanged in expressions, because they are in the final form of pointers in the compiler and can all perform subscript operations. Because the subscript operation of the array is based on the pointer.All arrays passed to the called function during function calling are rewritten as pointers by the compiler during compilation, even if they are declared as arrays by the called function parameters.To define an array as a function parameter, you can define it as an array or a pointer. No matter which method you choose, what you actually get inside the function is a pointer!
Exercises after class(If the prediction result is correct, you actually understand the arrays and pointers in the C language ):
Code
1 # Include < Stdio. h >
2
3 Char Ga [] = " Abcdefghi " ;
4
5 Void My_array_func ( Char Ca [ 10 ])
6 {
7 Printf ( " Sizeof array Param = % # x \ n " , Sizeof CA );
8 Printf ( " ADDR of array Param = % # x \ n " , & CA );
9 Printf ( " ADDR of (Ca [0]) = % # x \ n " , & (Ca [ 0 ]);
10 Printf ( " ADDR of (Ca [1]) = % # x \ n " , & (Ca [ 1 ]);
11 Printf ( " ++ Ca =%# x \ n " , ++ CA );
12 }
13
14 Void My_pointer_func ( Char * Pa)
15 {
16 Printf ( " Sizeof PTR Param = % # x \ n " , Sizeof Pa );
17 Printf ( " ADDR of PTR Param = % # x \ n " , & Pa );
18 Printf ( " ADDR of (PA [0]) = % # x \ n " , & (PA [ 0 ]);
19 Printf ( " ADDR of (PA [1]) = % # x \ n " , & (PA [ 1 ]);
20 Printf ( " ++ PA =%# x \ n " , ++ Pa );
21 }
22
23 Int Main ()
24 {
25 Printf ( " Sizeof Global Array = % # x \ n " , Sizeof Ga );
26 Printf ( " ADDR of Global Array = % # x \ n " , & Ga );
27 Printf ( " ADDR of (Ga [0]) = % # x \ n " , & (Ga [ 0 ]);
28 Printf ( " ADDR of (Ga [1]) = % # x \ n " , & (Ga [ 1 ]);
29 My_array_func (GA );
30 My_pointer_func (GA );
31 }
Output:
Output
sizeof Global Array = 0xa
ADDR of Global Array = 0x804a018
ADDR of (Ga [0]) = 0x804a018
ADDR of (Ga [1]) = 0x804a019
sizeof array Param = 0x4
ADDR of array Param = 0xbfd4ea50
ADDR of (Ca [0]) = 0x804a018
ADDR of (Ca [1]) = 0x804a019
++ CA = 0x804a019
sizeof PTR Param = 0x4
ADDR of PTR Param = 0xbfd4ea50
ADDR of (Pa [0]) = 0 X804a018
ADDR of (PA [1]) = 0x804a019
++ Pa = 0x804a019
success ~~