C language basics-Lesson 6-pointers and C language pointers
1 pointer 1.1 pointer 1.1.1 pointer Concept
The pointer variable is also a variable.
The pointer stores an address pointing to a memory space.
1.1.2 definition of pointer Variables
You can define a pointer variable pointing to a variable.
Int * p; // defines a pointer variable.
* P; // represents the actual data in the memory indicated by the pointer.
Remember, pointer variables can only store addresses. You cannot assign an int variable to a pointer directly.
Int * p = 100;
1.1.3 & obtain the address Operator
& Get the address of a variable in the memory
1.1.4 no type pointer
Defines a pointer variable, but does not specify which data type it points. You can convert void * to another type pointer by force conversion, or you can use (void *) to forcibly convert other types of pointer to void type pointer.
Void * p
1.1.5 NULL
NULL is defined as (void *) 0 in C language.
1.1.6 NULL pointer and wild pointer
A pointer to NULL is a NULL pointer, and no pointer to any variable address is a wild pointer.
1.1.7 pointer compatibility
The assignment between pointers is more rigorous than the assignment of common data types. For example, you cannot assign a double * value to an int *
In principle, pointers of the same type must point to the address of a variable of the same type. One type of pointer cannot point to another type of variable.
1.1.8 pointer to constant and pointer constant
Const char * p; // defines a pointer to a constant
Char * const p; // defines a pointer constant, whose content cannot be changed once initialized
1.1.9 relationship between pointers and Arrays
A variable has an address, and an array contains several elements. Each element has an address in the memory.
Int a [10];
Int * p =;
Compare whether the IP addresses of p and & a [0] are the same
1.1.10 pointer operation
Pointer operation is not a simple integer addition and subtraction, but an operation that uses the number of bytes occupied by the Data Type pointed to by the pointer in the memory as a multiple.
Char * p;
P ++;MovedSizeof (char)So many bytes
Int * p1;
P1 ++MovedSizeof (int)So many bytes
Value assignment: int * p = &;
Evaluate: int I = * p;
Int ** pp = & p;
Add (subtract) an integer to the pointer: p + 3; p-3;
Increase (decrease) pointer value p ++, p --
Difference value. p1-p2 is usually used to calculate the distance between two elements in the same number group.
Compare p1 = p2, which is usually used to compare whether two pointers point to the same position.
1.1.11 use array elements through pointers
P + 1 represents & a [1], or p [1] can be used directly to represent a [5]
P + 5 stands for & a [5]
P ++
1.1.12 pointer Array
Int * p [5];
1.1.13 pointer to pointer (second-level pointer)
A pointer is a variable. Since a variable also has a memory address, you can define a pointer to the pointer.
Int I = 10;
Int * p1 = & I;
Int ** p2 = & p1;
Printf ("% d \ n", ** p2 );
Level 3 or even multi-level pointers can also be defined. Multi-level pointers can be defined in the C language, but excessive pointer levels will increase the complexity of the Code. During the examination, multi-level pointers may be taken, but a maximum of three levels can be used in actual programming, however, Level 3 pointers are not commonly used. level 1 and level 2 pointers are widely used.
1.1.14 pointer to a two-dimensional array
Int buf [3] [5] |
Two-dimensional array name. buf indicates the first address of the array. |
Int (* a) [5] |
Define a pointer variable a pointing to the int [5] Type |
A [0], * (a + 0), * |
0 rows, 0 column element address |
A + 1 |
First line address |
A [1], * (a + 1) |
1st rows, 0 column element address |
A [1] + 2, * (a + 1) + 2, & a [1] [2] |
1st rows and 2 column element addresses |
* (A [1] + 2), * (a + 1) + 2), a [1] [2] |
Values of elements in the second row and two columns |
1.1.15 Use Pointer variables as function parameters
Function parameters can be pointer types ., It transfers the address of a variable to another function.
The pointer parameter of a function can be used to indirectly modify the value of a real parameter.
1.1.16 one-dimensional array name as function parameter
When the array name is used as a function parameter, the C language interprets the array name as a pointer
Int func (int array [10]);
1.1.17 two-dimensional array name as function parameter
When a two-dimensional array is used as a function parameter, the first subscript is not specified.
Int func (int array [] [10]);
It is not uncommon to use two-dimensional arrays as function parameters.
1.1.18 the const keyword protects the array content
If an array is passed as the form parameter of the function, the array content can be modified inside the called function. Sometimes this is not expected, so you must use the const parameter for the form parameter.
Func (const int array [])
1.1.19 pointer as function return value
1.1.20 pointer to function
A pointer can point to a variable, an array, or a function.
A function is allocated an entry address during compilation. The entry address is the pointer of the function, and the function name represents the entry address of the function.
Function pointer definition: int (* p) (int); // defines a pointer to the int func (int n) type function address.
1 Define the function pointer variable in the form of: function return type (* pointer variable name) (parameter list)
2. functions can be called through function pointers.
3int (* P) () indicates a function, but not a fixed function.
Void man () { Printf ("Smoke \ n "); Printf ("drinking \ n "); Printf ("cards \ n "); } Void woman () { Printf ("makeup \ n "); Printf ("Shopping \ n "); Printf ("Online Shopping \ n "); } Int main () { Void (* p )(); Int I = 0; Scanf ("% d", & I ); If (I = 0) P = man; Else P = woman; P (); Return 0; } |
When the callback function is dynamically bound to the runtime, a large number of pointers to the function are used.
1.1.21 use the pointer to the function as the function parameter
Using the function pointer as a parameter of another function is called a callback function.
Int max (int a, int B) { If (a> B) Return; Else Return B; } Int add (int a, int B) { Return a + B; } Void func (int (* p) (int, int), int a, int B) { Int res = p (a, B ); Printf ("% d \ n", res ); } Int main () { Int I = 0; Scanf ("% d", & I ); If (I = 0) Func (max, 10, 20 ); Else Func (add, 10, 20 ); Return 0; } |
1.1.22 memset, memcpy, memmove Function
These three functions implement memory settings, memory copy, and memory movement respectively.
When using memcpy, make sure that the memory does not overlap.
# Include <string. h>
1.1.23 pointer Summary
Definition |
Description |
Int I |
Define Integer Variables |
Int * p |
Define a pointer variable pointing to an int |
Int a [10] |
Define an int Array |
Int * p [10] |
Defines an array of pointers. Each array element points to the address of an int variable. |
Int (* p) [10] |
Defines an array pointer to a pointer variable of the int [10] type. |
Int func () |
Defines a function. The returned value is int type. |
Int * func () |
Defines a function. The returned value is int * type. |
Int (* p )() |
Define a pointer to a function. The prototype of the function is no parameter and the return value is int. |
Int ** p |
Defines a pointer to an int, a second-level pointer. |
2 character pointer and string 2.1 pointer and string
In C, most string operations are actually pointer operations.
Char s [] = "hello world "; Char * p = s; P [0] = 'a '; |
2.2 access string arrays through pointers
Char buf [100] = "hello world "; Char * p = buf; // * (P + 5) = 'a '; // P [5] = 'B '; P + = 5; * P = 'C '; P [3] = ''; Printf ("buf = % s \ n", buf ); |
2.3 The function parameter is char *
Void print_str (char * s) // If the parameter is a string, you do not need to include the second parameter. // Because the string is clearly ending with '\ 0', there are conditions in the function as the basis for loop termination. { Int I = 0; While (s [I]) { Printf ("% c", s [I ++]); } } |
2.4 pointer array as the main function parameter
Int main (int argc, char * argv []);
MainThe function is called by the operating system, soMainThe function parameters are also automatically filled in when the operating system calls them.
ArgcNumber of command line parameters
Argv indicates a specific parameter of the command line, which is of the char * type.