A pointer is a type of data in C, similar to other types in C, such as int and char. Since the pointer is a type, when we define this type variable, this type variable is called a pointer variable.
C has a pointer and points to it. Point: the pointer variable points to the memory unit expressed by the saved content (address or pointer.
A c pointer is a special variable, and the values stored in it are interpreted as an address in the memory.
In a computer, all data operations are performed through its address. Pointers make the program more efficient and less code.
When learning the pointer time, you must understand and distinguish the four senses of the pointer: the pointer type, the pointer type, the pointer value, and the memory occupied by the pointer itself.
The general form of pointer variable definition: type specifier * variable name. Type Description: indicates the Data Type of the variable pointed to by the pointer, And the pointer variable can only point to the Data Type of the variable. This is a feature of pointer variables.
*: Pointer variable of the variable. This is also a feature of pointer variables.
Variable name: the name of the pointer variable.
For example, char * pstu; * indicates that pstu is a pointer variable, and char is the data type of the variable pointed to by the pointer variable pstu. The entire statement defines a pointer variable pointing to a char type variable. But the specific pstu points to the char type variable, which depends on the value assigned to pstu when pstu is used.
Let's talk about the two operators that pointers have to say. Obtains the address and value operators *. The address of a variable can be obtained through the get address operator and the data stored in an address can be obtained through the value operator.
Before using pointer variables, define them first, and assign values to the pointer after initialization. Only values of compatible types can be assigned when values are assigned.
For example:
# Include
# Include
Void main ()
{
Int name, stuName = 1;
Int * pStu;
Int * pNew;
PStu = & stuName;
PNew = pStu;
Name = * pNew;
Printf ("% d \ n", stuName, * pStu, name, * pNew );
Printf ("% d \ n", & stuName, & pStu, & name, & pNew );
Printf ("% d \ n", pStu, pNew );
}
From the printed value, you can fully understand what is stored in the memory.
Simple modification:
# Include
# Include
Void main ()
{
Int name, stuName = 1;
Int * pStu = NULL;
Int * pNew = NULL;
PStu = & stuName;
PNew = * (& pStu );
Name = * pNew;
* PStu = 100;
Printf ("% d \ n", stuName, * pStu, name, * pNew );
Printf ("% d \ n", & stuName, & pStu, & name, & pNew );
Printf ("% d \ n", pStu, pNew );
}
The addition of pointers and integers. It is mainly used in arrays.
Int arr [] = {1, 2, 3, 4, 5 }; <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/Signature + signature + CjxwPsn5w/Signature/Kx7Hkwb + signature/Tw7eou/Signature + 1rjV68r91 + signature/LNrNK7yv2 + signature/ authorization + mho9a41evK/dfptcTL + authorization/zDx7HY0Ou + 39PQz + authorization/yz + DNrLXEyv2 + authorization/e3 + yAgKsr91 + nD + 1vK/authorization + authorization "Chain ", "Beijing", "LongMai "};
-) Constant pointer (points can be modified, points to the corresponding value cannot be modified)
Constants are adjectives, pointers are nouns, and pointer-centered partial positive structure phrases. In this case, a constant pointer is essentially a pointer, and a constant modifies it, indicating that this pointer is a pointer (variable) pointing to a constant ).
If the pointer points to a constant, the object cannot be changed.
In C/C ++, constant pointers are declared as follows:
1) const int * p;
2) int const * p;
Note that the object pointed to by the pointer cannot be modified through this pointer, but it can still be modified through the original declaration. That is to say, the constant pointer can be assigned the variable address, the constant pointer restricts the modification of variable values through this pointer.
For example:
Int a = 5, B = 6;
Const int * c = & a; // This is legal and the use of c is invalid.
C = & B; // valid
* C = 6; // invalid, but the value of the object pointed to by c can be modified as follows: a = 6;
Const int * d = & B; // B is a constant. d can point to B. it is legal to assign a value to B.
(2) pointer constants (points cannot be modified, and points to corresponding values can be modified)
A pointer is an adjective and a constant is a noun. This is a positive structure phrase centered on constants. Then, the essence of a pointer constant is a constant, and it is modified with a pointer, it means that the value of this constant should be a pointer.
The value of a pointer constant is a pointer. This value cannot be assigned because it is a constant.
In C/C ++, the pointer constant is declared as follows:
Int;
Int * const B = & a; // place const on the right of the pointer declaration Operator
As long as the const is on the right of the pointer declaration Operator, it indicates that the declared object is a constant, and its content is a pointer, that is, an address. The above statement can be read in this way. It declares a constant B whose value is the address of variable a (the address of variable a, isn't it a pointer to variable ).
Because a pointer constant is a constant, you must assign it an initial value when declaring it. Once assigned, this constant can no longer point to another address.
Although the value of a pointer constant cannot be changed, the object it points to is variable, because we do not limit that the object it points to is a constant.
Therefore, there is such a program:
Char * a = "abcde1234 ";
Char * B = "bcde ";
Char * const c = &;
The following operations are acceptable.
A [0] = 'X'; // we do not limit a to a constant pointer (pointer to a constant)
Or
* C [0] = 'X' // consistent with the preceding operation
3) pointer constant pointing to a constant (pointing to a constant cannot be modified or pointing to a corresponding value cannot be modified)
The pointer constant pointing to a constant is a constant, and the object it points to is also a constant.
Because it is a pointer constant, the object it points to is of course a pointer object, and it points to a constant, indicating that the object it points to cannot be changed.
In C/C ++, the statement is as follows:
Const int a = 25;
Const int * const B = &;
Function pointer: A function pointer is a pointer variable pointing to a function.
Data Type (* variable name )();
Int (* f) (int x);/* declare a function pointer */
F = func;/* assign the first address of the func function to the pointer f */
Pointer function: A pointer function is a function with a pointer. A function returns a pointer of a certain type.
The above is just a brief introduction to pointers. The complex usage of pointers will be discussed later...