Pointer:
I believe you are familiar with the following code:
Int
I = 2;
Int * P;
P = & I;
This is the simplest pointer application and the most basic usage. Next, let's get familiar with what is a pointer: first, a pointer is a variable, which saves not ordinary data, but the address of the variable. In the above Code, the pointer p Stores the address information of the integer variable I.
Next, let's take a look at how to define a pointer. Since a pointer is also a variable, its definition is also the same as that of other variables: int * P; * is an indirect addressing or indirect reference operator. In the above example, we also see a special operator &, which is an address-taking operator (in other cases, it is also a bitwise operator, & for the intersection operator ).
In the pointer definition above, we can see that the definition is an integer pointer. Do pointers still have types? The answer is yes, the pointer can only point to a specific type of object, that is, each pointer must point to a specific data type (the only exception: A pointer to the void type can store a pointer to any type, but it cannot indirectly reference itself .). For example, an int type pointer cannot point to a char type variable.
The following is a complete example to illustrate the simple application of pointers:
# Include <stdio. h>
Void main ()
{
Int
A, B, C, * P;
A = 1;
B = 3;
P = &;
B = * p + 1;
C = * (p + 1 );
Printf ("% d
% D/N ", a, B, c, * P + 3 );
}
Running result: 1 2-858993460 4
This is a complete example. You can debug it on your machine. Many people now use Microsoft's Visual Studio development environment, some people do not know how to write C Programs and debug C Programs in this development environment. For details, refer to the appendix.
In the above example, we can see two such expressions: B = * p + 1; and c = * (p + 1 ); the former means adding 1 to the content in the address referred to by P to B, which is equivalent to B = a + 1 ;, the latter is the address referred to by P plus 1 and then the address referred to by (p + 1) is assigned to C. Of course, we do not know what is put in the next address of P, therefore, a random value is output (this operation is very dangerous. Remember not to use an uncertain memory address ).