C language programming study notes-pointer,
C language programming learning notes-pointer.
8.1 what is a pointer
1. differentiate between pointers and pointer variables:
? Pointer: the address of a variable is called the "Pointer" of the variable, and the pointer is an address.
? Pointer variable: a variable is used to store the address of another variable. The pointer variable is the variable that stores the address.
2. Example:
The pointer to variable I is 2000. It cannot be said that the pointer to variable I is 2000.
8.2 pointer variable
Example:
# Include
Int main ()
{
Int a = 7, B = 6;
Int * p1; int * p2; // defines two pointer variables p1. p2 * indicates that the variable is a pointer variable.
P1 = &;
P2 = & B;
Printf ("a = % d B = % d \ n", a, B );
Printf ("p1 = % d p2 = % d", * p1, * p2); // "* p1 * p2" only indicates the variable pointed to by the pointer variable p1 and p2.
Return 0;
}
1. Definition: type name * pointer variable name int * p1, * p2; base type: Int pointer variable p1, p2;
For example, float * p1 // defines the float pointer of a float variable.
Char * p2 // pointer variable defining character type variables (char pointer)
Int * p3 = & a, * p4 = & B // initialize at the same time as defined
2. Description:
(1) When defining pointer variables, you must specify the base type, because the number of bytes in the memory and storage mode of different types are different.
(2) the pointer meaning of a variable includes two aspects;
? Address represented by the storage unit number
? Data Type of the storage unit to which it points
(3) the pointer variable can only store the address (pointer) and cannot pay an integer to a pointer variable,
For example, * p1 = 100 // invalid, p1 pointer variable, 100 is an integer
3. Reference: When referencing pointer variables, there may be three situations
(1) Assign p = & a to the pointer variable; // assign the address of a to the pointer Variable p;
(2) reference the Variable p = & a; * p = 1; // pay positive 1 to the variable currently referred to by p.
(3) reference the value of the pointer variable printf ("% o", p); eight bytes output the value of the pointer Variable p. If p points to a, It outputs the address of, that is, &
Example:
# Include
Int main ()
{
Int * p1, * p2, * p;
Int a = 5;
Int B = 6;
P1 = &;
P2 = & B;
If (
{
P = p1;
P1 = p2;
P2 = p;
}
Printf ("thenumber a = % d B = % d \ n", a, B );
Printf ("the max = % d the min = % d \ n", * p1, * p2 );
Getchar ();
Return 0;
}
Output:
(1) printf ("the max = % d the min = % d \ n", * p1, * p2); // p1, p2 points to the value of the Variable
(2) printf ("the max = % dthe min = % d \ n", p1, p2); // wrong
4. Use Pointer variables as function parameters
Purpose: Send the address of a variable to another function.
Example:
# Include
Void swap (int * p1, int * p2)
{
Int temp;
Temp = * p1;
* P1 = * p2;
* P2 = temp;
}
Int main ()
{
Void swap (int * p1, int * p2 );
Int * pp1, * pp2, * p;
Int a = 5;
Int B = 6;
Pp1 = &;
Pp2 = & B;
Printf ("thenumber a1 = % d b1 = % d \ n", a, B); // a1, b1 before swap
If (
{
Swap (pp1, pp2 );
}
Printf ("\ n ");
Printf ("AfterSwitch \ n ");
Printf ("thenumber a2 = % d b2 = % d \ n", a, B); // a2, b2 after swap
Printf ("the max = % d the min = % d \ n", * pp1, * pp2); // p1, p2 stores the address of a and B, point
Getchar ();
Return 0;
}
Before Swap: a = 5; B = 6
After Swap: a = 6; B = 5
Note: The value of a real parameter cannot be changed by changing the value of the pointer parameter.
8.3 use pointers to reference arrays
1. pointer to array elements
? The pointer to an array element is the address of an array element.
Example:
P = & a [0]; // The value of p is the address of a [0 ].
P = a; // The value of p is the address of the first element (that is, a [0]) of array.
Note:
? The array name does not represent the entire array. It only represents the address of the first element of the array.
? When defining a pointer variable, You can initialize int * p = & a [0]
? Pointer operation when referencing array elements
When the pointer points to an array element, it is allowed to add and subtract the pointer.
P + 1 P-1 P + + P --
Note:
(1) When performing the operation, the number of bytes occupied by an array element is not added to the address.
(2) If the initial value of P is & a [0], p + 1 and a + I point to the element whose serial number is I.
(3) [] is actually an address change operator, that is, a [I] calculates the address based on a + I and finds the value in the address unit.
2. reference array elements through pointers
? There are two methods to reference an array element:
(1) subscripts a [I] P233
(2) pointer Method * (a + I), or * (p + I). a array name. p is a pointer variable pointing to an array element.
# Include
Int main ()
{
Int a [10];
Int * p, I;
Printf ("please input10 numbers ");
For (I = 0; I <10; I ++)
Scanf ("% 2d", & a [I]);
For (p = a; p <(a + 10); p ++) // you cannot change p ++ TO a + =.
Printf ("% 2d", * p); // a represents the first address of the array element. It is a pointer type constant and cannot be implemented by a ++.
Return 0;
}
Note:
(1) You can change the pointer variable value to point to different elements.
(2) pointer variables pointing to Arrays can carry subscripts, because the processing method for the following objects during compilation is to convert them to addresses.
Processing p [I] As * (p + I) // not recommended
Difference obfuscation:
3. array name as function parameter
Differences:
? Real parameter array name: represents a fixed address, which is processed by pointer constants.
? Parameter array name: it is not a fixed address. It is processed by pointer variables.
Summary:
There are four situations for changing the element values in an array in a function:
4. Use a pointer to reference a multi-dimensional array
8.4 reference a string with a pointer
1. String reference
Strings are stored in character arrays. There are two methods to reference a string:
? Character array stores a string. You can use the array name and subscript to reference a character in the string. You can use the array name and format to declare % s to output the string.
? The character pointer variable points to a String constant and uses the character pointer variable to reference A String constant.
2. Character pointers as function parameters
3. Comparison between character pointer variables and character Arrays
(1) character array -- storing characters in Elements
Character pointer variable -- Address
(2) Assignment Method -- assign values to character pointer variables, but not to array names
(3) initialize and assign the initial value to the character pointer variable
(4) storage unit content:
? Character array-several units are allocated during compilation to store the values of each element.
? Character pointer variable -- allocate only one storage unit
(5) pointer variable value -- can be changed
Array name -- represents a fixed value (the first address of the element) and cannot be changed
Char * a = "I LOVE CHINAHAHAHA"; // space is also
A = a + 7;
Printf ("% s \ n", a); // valid
If you change
Char str [] = {"I LOVE CHINA HAHAHA"}; // a space
Str = str + 7;
Printf ("% s \ n", str); // invalid array name represents the address, constant, cannot be changed
(6) The values of each element in the character array can be changed by assigning values.
Chara [] = "gaobo ";
A [2] = 'D'; // valid
The content in the String constant to which the character pointer variable points cannot be changed.
Char * a = "gaobo ";
A [2] = 'D'; // invalid
8.5 pointer to function
1. function pointer: the starting address (Entry address) of the bucket is called the pointer of this function.
2. The function pointer variable calls the function:
(1) call a function by function name
(2) access the function pointed to by the pointer variable
3. pointer variable pointing to the Function
? Definition: type name (* pointer variable name) (function parameter table column)
Int (* p) (int, int );
Note:
B. Use
4. Use pointers to functions as function parameters
Pass the function address as a parameter to other functions
8.6 return the pointer to the Function
General Format: type name * function name (parameter table column)
8.7 pointer array and multiple pointers
1. pointer array: type name * array name [array length] int * p [4]
Note: int (* p) [4] // pointer variable pointing to a one-dimensional array
2. the pointer array is used as the main function parameter.
8.8 dynamic memory allocation and Its pointer Variables
1. dynamic memory allocation:
(1) Definition:
How to dynamically allocate or recycle the memory allocated to a bucket during execution. Unlike static memory allocation methods such as arrays, dynamic memory allocation requires pre-allocation of storage space. Instead, the system allocates the storage space in real time based on program requirements, and the allocated size is the size required by the program.
(2) how to create a dynamic memory allocation?
Void * malloc (unsigned int size) // allocate a continuous space with a length of size.
Volid * calloc (unsigned n, unsigned size) // allocate an n * size Space
Void free (void * p) // release the dynamic space allocated by the pointer Variable p.
Void * realloc (void * p, unsigned int size) // change the size of the space obtained
2. Void pointer type: pointing to null type or uncertain type
Legacy problems:
Character pointer variable and character array comparison
Understanding
Pointer variable value -- can be changed
Array name -- represents a fixed value (the first address of the element) and cannot be changed
Char * a = "I LOVE CHINAHAHAHA"; // space is also
A = a + 7;
Printf ("% s \ n", a); // valid
If you change
Char str [] = {"I LOVE CHINAHAHAHA"}; // a space
Str = str + 7;
Printf ("% s \ n", str); // invalid array name represents the address, constant, cannot be changed
Each element value in the character array can be changed by value assignment.
Char a [] = "gaobo ";
A [2] = 'D'; // valid
The content in the String constant to which the character pointer variable points cannot be changed.
Char * a = "gaobo ";
A [2] = 'D'; // invalid