For beginners C Most friends of language , Maybe I tripped over the pointer more or less . For example, let's look at the following code ( Functions in a piece of code )
Is it hard to watch . Let's take a brief look at pointers .
1. What pointer ?
Pointers are used to store variable addresses .
For example, here for variables a Fetch address ——&a, We got an address 0x012FF8C0
Since it is a value , We can define a variable to store it , This variable is called a pointer variable ( Variable to store address ).
int* Is the pointer type , It's the same as what we usually call an integer , float , The character type is the same , Also char*,short* etc . It's just a pointer modifier .
We can see that the two addresses above are the same , Description pointer variable p Got it a Address of . When printing, you must distinguish what you want to print , If you want to print a Values in variables , It should be like this :
*p Indicates dereference , Which means that p Get the value in the address pointed to , here p point a Address of ,a In the address of 0.
If we want to change a Values in variables , You can assign a value to *p, as follows :
This can be interpreted as p The value of the address pointed to is changed into 20
Here we have a general idea of what a pointer is .
2. Some relations of pointers
(1) Pointer +- integer
for example :
there p+1 Indicates that the memory skipped four bytes , So pointer variables p+- Integer indicating how many bytes memory space is moved ( be careful : If this is changed to char* p=&a If , Only one byte skipped )
(2) Pointers and arrays
This is undoubtedly very important knowledge , Again, we can access arrays through pointer variables , We know that the array parameter passes the address of the first element of the array , as follows :
Now that we have the address of the first element of the array , So we want to print a value in the array , You can access the corresponding value by changing the pointer variable , as follows :
To change the value in the array element, you can also change it according to the pointer variable , as follows :
We changed all the values in the array to 8
(3) Pointers and functions
Same as before , To access an address, we need to define a pointer variable , The same is true in functions , as follows :
Through this comprehensive application , You should have a certain understanding of the pointer .
Technology