C language 12th round: Pointer & #160; [learning objective] 1. & #160; & #160; & #160; & #160; & #160; & #160; & #160; & #160; Pointer 2. & #160; & #160; & #160; & #160; & #160; & #160; & #160; & #160; Pointer and array & #160; a: & #160; pointer concept & #160; & #160
C language 12th round: Pointer
[Learning objectives]
1. pointer
2. pointers and arrays
A: pointer concept
Memory storage units are sorted by byte. each byte is encoded with an serial number, which is called an address. Since we can find the desired memory unit through the address, we turn the address into a pointer. A pointer is a special variable. the value stored in it is interpreted as an address in the memory.
Purpose:
(1) pointers can effectively represent complex data structures, such as queues, stacks, and linked lists.
(2) the pointer can process the memory address like the assembly and provide support for dynamic memory allocation.
(3) pointers facilitate the use of arrays and strings and improve program efficiency
B: pointer variable definition
Data type * pointer variable;
For example:
Int * p2;/* p2 refers to the int type pointer variable */float * p3;/* p3 refers to the float type pointer variable */char * p4; /* p4 is a pointer variable pointing to the char type */
Description: The data type is not the type of the variable, but the type of the target variable that the variable points. Pointer variables can only point to variables of the same type.
// Pointer definition # include
Int main (void) {// wild pointer: it is not a NULL pointer, it is a pointer to the released or accessed spam memory int * ptr1; // ptr1 is an int pointer, wild pointer, which contains the spam address char * ptr2; // ptr2 is the char pointer and the wild pointer, which contains the spam address // compiled in VC, the following message is displayed: ptr1 and ptr2 do not initialize float * ptr3 = NULL; // ptr3 is a float pointer, not a wild pointer, and has been initialized as NULL // view the address printf ("(ptr1) = % p \ n ", ptr1); printf (" (ptr2) = % p \ n ", ptr2); printf (" (ptr3) = % p \ n ", ptr3); return 0 ;}
C: pointer assignment (note: the type must be matched)
(A) use the address operator &:
For example:
Int a = 133; int * p1; p1 = & a; // use the address operator to assign the value of a to the pointer p1
(B) assign a pointer variable that has been pointed to another pointer variable.
For example ):
Int * p2;
P2 = p1; // p1 and p2 both point to variable
(C) assignment of pointers and arrays
Int a [5], * pa; pa = a; // (array name indicates the first address of the array, so you can assign the pointer variable pa to the array) // You can also enter: pa = & a [0]; // The address of the first element of the array is also the first address of the entire array. // of course, you can also initialize the value assignment method: int a [5]; int * pa =;
(D) assign values to strings and pointers.
For example:
Char * pc; pc = "C Language"; // or use the initialization value: char * pc = "C Language ";
PS: You cannot directly assign a number to a pointer variable!
For example:
Int * p; p = 1000; // type mismatch, error prompted // improved method P = (int *) 100; // forced type conversion.
D: use of & and *
(A) the address of a variable can be obtained by using the valid accessor.
For example:
Int a; int score [5] = {80, 90, 97, 98, 63}; & a, & score [0]; // This operation is legal & (a + 5); // This operation is invalid because it is out of the permitted range & a = 123; // This operation is invalid. if necessary, it must be forcibly converted & score; // This operation is invalid. score itself indicates the first address of the score.
(B) use the * operator to indirectly access the value of the target variable pointed to by the pointer (the type must match)
Itn a = 234; int * p1; p1 = & a; // p1 points to the array aprintf ("% d \ n", * p ); // symbol * is used to extract the content from the address.
E: add and subtract the pointer and integer
Meaning: pointer movement
For example, p + n p-n p ++ p -- p
PS:
(1) where n is an integer, and the address cannot be moved to decimal places.
(2) addition indicates that the pointer p moves in the direction of increasing the address.
(3) subtraction indicates that the pointer p moves in the direction of address reduction.
(4) the length of movement is determined by the computer.
For example, if p is a pointer to the type, and n is an integer expression, p + (or-) n is a new address. The value is p + (or-) n * sizeof (type ).
F: Pointer and array
Since the storage units of each element in the array are continuously allocated, you can use pointers to access the array. the array name is the first address of the number.
For example, int a [] = "abcdefg ";
A is the first address of the array, which is equivalent to & a [0].
PS: through the receiving address, you can quickly and conveniently access other elements in the array, as follows:
First Address + offset
A [I] can be converted to: * (a + I), * (& a [0] + I)
Comparison between pointers and arrays |
Pointer |
Array |
The address for storing data. any data stored in pointer variables will be processed as addresses. |
Save the data. the array name is the first address of the array's first element. |
Indirectly access the data, obtain the content (address) in the pointer variable, and then extract the data from this address. You can use pointers to access data or subscripts to access data. |
Access data directly. You can access data through pointers or subscripts. |
Usually used for dynamic data structures |
Usually used to store a fixed number |
Use the malloc, calloc, recalloc, and free functions to allocate memory. |
You cannot delete the allocated memory or data. |
[Smile at your fingertips] Errors are inevitable. I hope you can get your corrections ^-^
Reprinted when retaining the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself