C Language 12th round: Hands
"Learning goals"
1. Pointers
2. Pointers and Arrays
A: the concept of pointers
Memory storage units are sorted by byte, each byte is numbered and we call it an address. Since we can find the desired memory unit through the address, we make the address a pointer. The pointer is a special variable, and the value stored in it is interpreted as an address in memory
Role:
(1) Pointers can effectively represent complex data structures such as queues, stacks, linked lists, etc.
(2) Pointers can handle memory addresses like assemblies, providing support for dynamic memory allocations
(3) pointer can be used to facilitate the use of arrays and strings, improve the efficiency of the program
B: definition of pointer variable
Data type * pointer variable;
Such as:
INT*P2; /*P2 is a pointer variable that points to int type */ float*p3; /*P3 is a pointer variable that points to float type */ CHAR*P4; /*P4 is a pointer variable that points to char type */
Description : The data type does not refer to the type of the variable itself, but to the type of the target variable that the variable is pointing to. pointer variables can only point to variables of the same type .
Pointer definition # include <stdio.h> int main (void) { //Wild pointer: Not a null pointer, is a pointer to the released or to the garbage memory int *ptr1; PTR1 is the int pointer, the wild pointer, inside is the garbage address char *ptr2;//PTR2 is a char pointer, the wild pointer, inside the garbage address //compiled in VC, will prompt Ptr1 and PTR2 did not initialize float *ptr3= NULL; PTR3 is a float-type pointer, not a wild pointer, has been initialized to null //view address printf ("(ptr1) =%p\n", ptr1); printf ("(ptr2) =%p\n", ptr2); printf ("(PTR3) =%p\n", PTR3); return 0; }
C: Assignment of Pointers (Note : type to match )
(a) using the address operator &:
Such as:
int a =133;int * p1;p1= & A; Use the address operator to assign a value to the pointer p1
(b) Assigning a pointer variable that already has pointers to another pointer variable
If (connect to above):
int* P2;
P2=P1; P1 and P2 also point to variable a
(c) Assignment of pointers and arrays
int a[5],*pa; Pa=a; (The array name represents the first address of the array, so you can assign a pointer variable PA to the array) //can also be written as: pa=&a[0]; The address of the first element of the array is also the first address of the entire array, and /or the method of initializing the assignment: int a[5]; int *pa=a;
(d) Assignment of strings and pointers.
For example:
char *pc; pc= "C Language"; or assignment with initialization: char *pc= "C Language";
PS: It is not allowed to assign a number directly to the pointer variable!
Such as:
int *p; p=1000; Type mismatch, hint error //Improved mode p= (int *) 100;//forced type conversion.
D: & and the * the Use
(a) The address of a variable can be obtained by legal use of the address character
Such as:
int A; int score[5]= {n , a, 98, and A. &a,&score[0]; The operation is lawful & (a+5); This operation is illegal, cross-border &a=123; The operation is illegal, mandatory conversions required if necessary &score; The operation is illegal, score itself represents score's first address
(b) Use the * operator to indirectly access the value of the target variable to which the pointer is pointing ( type to match )
ITN a = 234;int * p1;p1=& A; P1 points to the array aprintf ("%d\n", *p); The symbol * is to remove the contents of the address.
E: The pointer is added to the integer minus
Meaning: Represents the movement of a pointer
such as: P+n p-n p++ p--++p--p
Ps:
(1) where n is an integer, the move of the address cannot be a decimal.
(2) The addition indicates that the pointer p moves in the direction of the address increase.
(3) Subtraction means that the pointer p moves in the direction of the address decrease
(4) As for the length of the move, it is determined by the computer.
such as: Set P is a pointer to type (type), n is an integer expression, then p+ (or-) n is a new address. The value is p+ (or-) n*sizeof (type).
F: Pointers and Arrays
Since the storage units for each element in the array are continuously allocated, you can access the array with a pointer, which is the first address of the number.
such as: int a[]= "ABCDEFG";
A is the first address of the array, equivalent to &a[0]
PS: Through the address, you can quickly and easily access the array of other elements, the method is as follows:
First address + offset
a[I] can be converted to: * (A+i), * (&a[0]+i)
Comparison of pointers to arrays |
Pointer |
Array |
Save the address of the data, any data stored in the pointer variable will be treated as an address. |
Saves the data, and the array name is the first address of the first element of the group. |
Access data indirectly by getting the contents (address) of the pointer variable, and then extracting the data from that address. Pointers can be used to access data in the form of pointers, or they can be accessed using the subscript form. |
Direct access to data. You can access the data in the form of pointers, or you can use subscripts to access the data. |
Typically used for dynamic data structures |
Typically used to store a fixed number of |
allocating memory using the Malloc,calloc,recalloc and free functions |
Cannot delete allocated memory, can delete data |
"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^
When reproduced, keep the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself
C Language 12th round: pointers