directory of this document
- Direct reference
- First, what is a pointer?
- Second, the definition of the pointer
- Third, the initialization of the pointer
- Four, pointer operators
- V. Examples of use of pointers
- Vi. questions relating to pointers
Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore
Pointers are a very important type of data in C, and if you say that you have a good study in c except for pointers, you simply say you have not learned C language. What exactly is a pointer? Let's look at a concept first.
Go back to the top of the direct reference 1. Recall how we changed the value of a variable before? We used the variable name to refer to the variable directly and then assign the value:
char a;a = 10;
2. It seems very simple, in fact, how to operate inside the program? In fact, the program read and write to the variable, in fact, the variable is located in the storage space to write or fetch data. In terms of the above code, the system automatically converts the variable name A to the storage address of the variable, locates the storage space of variable A according to the address, and then puts the data 10 into the storage space of variable A in 2 binary form. 3. The variable name refers to the variable, the system automatically completes the conversion between the variable name and its storage address, called the variable "direct reference" way back to the top one, what is a pointer?
1. We already know that "direct reference" is a direct read and write variable by variable name
There is also an "indirect reference" in the 2.C language (in the case of variable a): first, place the address of variable A in another variable, for example, in variable B, and then indirectly refer to variable a by variable B to read and write the value of variable a indirectly. This is the "indirect reference".
If the program modifies the value of a by "indirect reference", you can do this by getting the address ffc2 of the variable b from the variable name B, taking out the content stored in variable b ffc1, which is the address of variable a, and then finding the storage space of a in accordance with the address of variable a FFC1, and then modifying the data inside.
3. Sum up: Variables used to store variable addresses are referred to as "pointer variables". In the above case, variable B is a "pointer variable", and we can say that pointer variable b points to variable a.
Back to top second, the definition of the pointer
General form: class name identifier * pointer variable name;
int *p;float *q;
- "*" is a specifier that indicates that the variable is a pointer variable and cannot be omitted, but it is not part of the variable name
- The preceding type identifier represents the type of the variable that the pointer variable points to, and can only point to a variable of this type
Back to top three, the initialization of the pointer 1. Define and initialize first
1//Define a variable of type int a2 int a = 10;3 4//Define a pointer variable P5 int *P;6 7//Assign the address of variable A to the pointer variable p, so the pointer variable p points to the variable a8 p = &a;
Note that line 8th, assigned to P, is the address of variable a &a
2. Initialize at the same time as defined
Defines a variable of type int aint a = 10;//defines a pointer variable p//and assigns the address of variable A to the pointer variable p, so the pointer variable p points to the variable aint *p = &a;
3. Attention to initialization
The pointer variable is used to hold the variable address, do not give it arbitrarily assigned a constant. The following wording is wrong.
int *p; p = 200; This is wrong.
Back to top four, pointer operators
1. Assigning a value to a variable pointed to by the pointer
1 char a = 10; 2 printf ("Before modification, value of a:%d\n", a); 3 4//pointer variable p points to variable a 5 char *p = &a; 6 7//The value of variable A is indirectly modified by the pointer variable p 8 *p = 9; 9 printf ("Modified, value of a:%d", a);
When the program has just finished executing the 5th line of code, the approximate distribution in memory is like this
, the A value is the 10,P value is the address of variable a FFC3.
Notice that the 5th and 8th lines have a "*" and their meanings are different:
(1) the "*" in line 5th is used to indicate that p is a pointer variable
(2) The 8th line of "*" is a pointer operator, where the *p represents a P-value ffc3 this address to access the corresponding storage space, that is, the storage space of variable A, and then the right value 9 is written to this storage space, equivalent to a = 9;
The output is:, it can be found that we indirectly modified the value of variable A through the variable p.
2. Remove the value of the variable pointed to by the pointer
The pointer operator can also be used to take a value in addition to the assignment
1 char a = 10;2
Output: The *p in line 6th means: Access the corresponding storage space according to the P value (that is, the address of variable a) and take out the stored content (that is, the value of the variable A is taken), assigned to value
3. Use note
Do not assign a value to a pointer variable before it points to a definite address. The following wording is wrong.
int *p; *p = 10; This is wrong.
The pointer variable should be assigned after it points to a certain variable. The following wording is correct.
Define 2 int variable int a = 6, b;//define a pointer variable to variable b pint *p;p = &b;//assigns the value of a to the variable b*p = A;
Back to top v. Examples of use of pointers 1. Example 1
In front of us through the pointer variable p indirectly access to the variable A, in some people's opinion, think pointer variable good silly B, directly with the variable name A to access the variable a not good, why bother. Don't worry, let's take an example to see what the pointer can do.
Now there is a requirement: Write a function swap, receive 2 integer parameters, function is to swap the values of two arguments.
1> If you haven't learned the pointers, you might write them like this.
1 void Swap (char v1, char v2) {2 printf ("Before replacement: v1=%d, v2=%d\n", V1, v2); 3 4 //define an intermediate variable 5 char temp; 6
7 //Exchange V1 and V2 value 8 temp = v1; 9 v1 = v2;10 v2 = temp;11 printf ("After replacement: v1=%d, v2=%d\n", V1, v2); 1 3}14 int main () , C = 9;18 printf ("Pre-change: a=%d, b=%d\n", A, b), and (A, b); 21
22 printf ("After replacement: a=%d, b=%d", A, b); return 0;24}
Output: Although the values of V1 and V2 are swapped, the values of variables A and B are not exchanged at all. Because the base data type is simply passed to the parameter as a function argument, the change of the formal parameter does not affect the argument.
We can briefly analyze this process:
* In line 20th, the values of variables A and B are passed to the two parameters of the swap function V1, v2
* In line 8th, the value of V1 is assigned to the TEMP
* In line 9th, the value of V2 is assigned to V1
* In line 10th, the value of temp is assigned to V2
In this way, the values of V1 and V2 have been exchanged, but the values of a and B have not changed.
2> If you learn a pointer, you should write it like this.
1 void Swap (char *v1, char *v2) {2 //Intermediate variable 3 char temp; 4 5 //Remove the value of the variable that V1 points to 6 temp = *V1; 7 8 Remove the value of the variable pointed to by the V2 and assign it to the variable pointed to by v1 9 *v1 = *v2;10 one //Assign to the V2 point variable, *v2 = temp;13}14 int main (), 17
char A = ten, B = 9;18 printf ("Pre-replacement: a=%d, b=%d\n", A, b), " swap" (&a, &b); ("After replacement: a=%d, b=%d", A, b); return 0;24}
First look at the output: the values of the variables A and B are finally switched over.
Explain:
(In a 16-bit compiler environment, a pointer variable takes 2 bytes)
* Note the 20th line first, passing is the address of the variable. So the parameter v1 of the swap function points to the variable a,v2 points to the variable B
* The 6th line of code is to take out the value of the variable V1 points to, that is, the value of variable a: 10, then assign to the variable temp
* The 9th line of code is to take out the value of the variable (variable B) that V2 points to, and then assign the value to the variable that V1 points to (variable a)
* The 12th line of code is to assign the value of the TEMP variable to the variable that v2 points to (variable B)
Believe that you have felt the strength of the pointer, if there is no pointer, the inside of a function can not change the external arguments at all.
2. Example 2
Next, give a practical example of a pointer. By default, a function can have only one return value, and with pointers, we can implement a function that has a "multiple return value."
Now there is a requirement: Write a function Sumandminus, you can simultaneously calculate 2 integer and the difference, after the function is finished, return and with the difference (note, here to return 2 values)
Calculates the difference of 2 integers and the difference int sumandminus (int v1, int v2, int *minus) { //), and assigns a value to the variable that the pointer points to *minus = v1-v2; Computed and, and returned and return V1 + v2;} int main () { ///defines 2 int type variable int a = 6, B = 2; Define 2 variables to receive separately and sum with difference int, minus; Call function sum = Sumandminus (A, B, &minus); Print and printf ("%d+%d=%d\n", A, b, sum); Print poor printf ("%d-%d=%d\n", A, b, minus); return 0;}
Output:, and with the difference are computed by the same function and returned. and is the direct return value of the function, the difference is returned indirectly through the function's 3rd pointer parameter.
So with pointers, we can let the function have an "infinite" return value.
Back to the top six, questions about pointers
Just after learning the pointer, there may be a lot of doubts, here I listed a few common doubts it.
1. How many bytes of memory does a pointer variable occupy? Does the occupied space change following the type of the variable being pointed to?
In the same compiler environment, a pointer variable occupies a fixed amount of memory space. For example, in a 16-bit compiler environment, any pointer variable consumes only 2 bytes and does not change depending on the type of the variable being pointed to.
2. Since each pointer variable occupies the same amount of memory space and is stored as an address, why are pointer variables typed? And can only point to one type of variable? For example, a pointer to an int type, a pointer to a char type.
In fact, I think the question is the same as "why arrays are divided into types".
* Look at the code below and use the pointer p to read the value of the variable C
1 int i = 2;2 char c = 1;3 4//define a pointer to char type 5 char *p = &c;6 7//Remove 8 printf ("%d", *p);
This output should be difficult for everyone:, can be successfully read.
* If I change the code for line 5th, use a pointer p that should point to the INT type variable, point to Variable C of type Char
int *p = &c;
Let's take a look at the output: the original value of C is 1, now it's 513, what's going on? This is based on memory.
Depending on the order in which the variables are defined, these variables are roughly arranged in memory:
Where the pointer variable p and INT type variable i each account for 2 bytes, the char type C takes one byte, p points to C, so the P-value is the address of C
1> Initially, we used Char *p to point to the variable C. When using *p to get the value of the variable C, since the pointer p knows that the variable c is a char type, it starts reading 1 bytes of data from the FFC3 address: 0000 0001, which is 10 binary is 1
2> later, we use int *p to point to variable C. When using *p to get the value of the variable C, since the pointer P considers the variable C to be of type int, it starts reading 2 bytes of data from the FFC3 address: 0000 0010 0000 0001, 10 is 513
It can be seen how important it is to classify pointers, and a pointer is best to point to only one type of variable, which is the safest.
"C Language" 11-pointers