Pointer essence analysis
- Variables Review: variable in the program is but another name for a storage space, it is not necessary to use this storage space by the alias?
- Another variable may be changed by the pointer
- The nature of pointers to variables (as long as the variables, there is an address), but say that this variable is special, it is stored in the variable address value
- When pointer declaration, * represents the declared variable number of pointers
- When using the pointer, the pointer value takes asterisk indicates points to memory space
- * Similar to a key number, the memory can be opened by the key, read the value in memory
- Different pointers, memory occupied by the same. All 32-bit system, the pointer bytes are occupied 4
Call-by-call and pass-by
- A pointer is a variable, you can declare a pointer parameter
- When the internal body of a function to change the value of the argument, it is necessary to use a pointer parameter
- Function call, the argument value is copied to the parameter
- Pointer function parameters for complex data types
Constant pointer
. 1 const int * p; // p variable content of non-variable p at 2 int const * p; // p variable, the content pointed to by p immutable . 3 int * const p; // p immutable, p points the contents of the variable . 4 const int * const p; // p and p points are not variable content
Formulas: from the left and right finger , when const * number appears when the pointer to the left of the data is constant , when const appears to the right of the pointer itself is constant asterisk
1 #include <stdio.h> 2 int main() 3 { 4 int i = 0; 5 const int *p1 = &i; 6 int const *p2 = &i; 7 int* const p3 = &i; 8 const int * const p4 = &i; 9 p1 = NULL; //ok 10 *p1 = 1; //error 11 p2 = NULL; //ok 12 *p2 = 1; //error 13 p3 = NULL; //error 14 *p3 = 1; //ok 15 p4 = NULL; //error 16 *p4 = 1; //error 17 return 0; 18 }
operation result:
1> ------ Rebuild All started: Project: constants and pointers, configuration: Debug Win32 ------
1> main.c
1> j: \ c language exercises \ constants and pointers \ constants and pointers \ main.c (15): error C2166 : left value specifies const Object
1> j: \ c language exercises \ constants and pointers \ constants and pointers \ main.c (18): error C2166 : left value specifies const Object
1> j: \ c language exercises \ constants and pointers \ constants and pointers \ main.c (20): error C2166 : left value specifies const Object 1> j: \ c language exercises \ constants and pointers \ constants and pointers \ main.c (23): error C2166 : left value specifies const Object 1> j: \ c language exercises \ constants and pointers \ constants and pointers \ main.c (24): error C2166 : left value specifies const object is ========== Rebuild All: Success 0 , a failure, 0 skipped ==========