In C ++/C Programs, pointers and arrays can be replaced with each other in many places, which leads to the illusion that the two are equivalent.
An array is either created in a static storage area (such as a global array) or on a stack. The array name corresponds to (rather than pointing to) a piece of memory, and its address and capacity remain unchanged during the lifetime, only the content of the array can be changed.
A pointer can point to any type of memory block at any time, and its feature is "variable". Therefore, we often use pointers to operate dynamic memory. Pointers are far more flexible than arrays, but they are more dangerous.
The following uses a string as an example to compare the features of pointers and arrays.
1Modify content
In example 1, the size of character array a is 6 characters, and its content is hello \ 0. The content of a can be changed, for example, a [0] = 'x '. The pointer p points to the constant string "world" (in the static storage area with the content of world \ 0). The content of the constant string cannot be modified. In terms of syntax, the compiler does not think that the statement p [0] = 'X' is inappropriate, but this statement attempts to modify the content of the constant string and causes a running error.
Char a [] = "hello "; A [0] = 'X '; Cout <a <endl; Char * p = "world"; // note that p points to a constant string P [0] = 'X'; // the compiler cannot find this error. Cout <p <endl; |
Example 1 modify the array and pointer content
2ContentCopy and Comparison
The array name cannot be directly copied or compared. In Example 2, if you want to copy the content of array a to array B, you cannot use statement B = a. Otherwise, a compilation error will occur. Use the standard library function strcpy for replication. Similarly, if the content of B and a is the same, it cannot be determined by if (B = a). The standard library function strcmp should be used for comparison.
Statement p = a does not copy the content of a, but assigns the address of a to p. To copy the content of a, use the library function malloc as p to apply for a memory with a capacity of strlen (a) + 1 characters, and then use strcpy to copy strings. Similarly, the statement if (p = a) compares not the content but the address, and should be compared using the database function strcmp.
// Array... Char a [] = "hello "; Char B [10]; Strcpy (B, a); // B = a cannot be used; If (strcmp (B, a) = 0) // if (B = a) cannot be used) ... |
// Pointer... Int len = strlen (); Char * p = (char *) malloc (sizeof (char) * (len + 1 )); Strcpy (p, a); // do not use p =; If (strcmp (p, a) = 0) // do not use if (p =) ... |
Example 2 copying and comparing the array and pointer content
3Computing memory capacity
The sizeof operator can be used to calculate the array capacity (number of bytes ). In Example 3 (a), the value of sizeof (a) is 12 (do not forget '\ 0 '). The pointer p points to a, but the value of sizeof (p) is 4. This is because sizeof (p) obtains the number of bytes of a pointer variable, which is equivalent to sizeof (char *) rather than the memory capacity referred to by p. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory.
Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.In Example 3 (B), sizeof (a) is always equal to sizeof (char *) regardless of the size of array *).
Char a [] = "hello world "; Char * p =; Cout <sizeof (a) <endl; // 12 bytes Cout <sizeof (p) <endl; // 4 bytes |
Example 3 (a) Calculate the memory capacity of arrays and pointers
Void Func (char a [1, 100]) { Cout <sizeof (a) <endl; // 4 bytes instead of 100 bytes } |
Example 3 (B) the array degrades to a pointer
---------------------------------
1. String macro # define CONST_STR "const str" the Macro will be replaced with the actual value during pre-compilation.
2. the array name corresponds to a piece of memory. Its address and capacity will not change during the lifecycle, and the content in the array can be changed.
3. the pointer points to a piece of memory. If it points to a String constant (RO), the content cannot be modified. If you have applied for a piece of memory and copied a String constant, you can modify the content.
Stdioh
- # Include h
- # Include unistdh
- # Include stdlibh
- # Define CONST_STR
- Typedef void string_test_routine_t
- Void pointer_strcpy_macrovoid
- Char p
- P charmalloc100
- Strcpyp CONST_STR
- Printf
- Printf
- P0
- Printf
- Void pointer_strcpy_stringvoid
- Char p
- P charmalloc100
- Strcpyp
- Printf
- Printf
- P0
- Printf
- Void pointer_macro_stringvoid
- Char p CONST_STR
- Printf
- Printf
- P0
- Printf
- Void pointer_stringvoid
- Char p
- Printf
- Printf
- P0
- Printf
- Void array_macro_stringvoid
- Char a100 CONST_STR
- Printf
- Printf
- A0
- Printf
- Void array_stringvoid
- Char a100
- Printf
- Printf
- A0
- Printf
- Void array_strcpy_stringvoid
- Char a100
- Strcpya
- Printf
- Printf
- A0
- Printf
- Void array_strcpy_macrovoid
- Char a100
- Strcpya CONST_STR
- Printf
- Printf
- A0
- Printf
- Static string_test_routine_t func_base
- Array_strcpy_string
- Array_macro_string
- Array_string
- Array_strcpy_macro
- Pointer_macro_string
- Pointer_string
- Pointer_strcpy_macro
- Pointer_strcpy_string
- # Define NUM_FUNC sizeof func_base sizeoffunc_base0
- Void test_stringvoid
- Pid_t pid
- I 0
- String_test_routine_t func func_base
- I 0 I NUM_FUNC I
- Pid fork
- Pid 0
- Printf
- Pid 0
- Funci
- 0
- Sleep 1
- Main
- Test_string
- Return 0