The presence of C language pointers makes the control of hardware and the flexibility of C language greatly improved.
However, there are many difficult problems in the use of pointers.
#include <stdlib.h>#include<stdio.h>//The function here is an example of a pointer doing a parameter, knowing that this feature can compensate for the ability of a C language to have only one return value. voidSWAP1 (int*PA,int*pb) { intT =*PA; *pa=*PB; (P =t;}//The main () function must return a numberintMain () {intA = the; intb=Ten; int*T; T=&b; A=b; b=*T; printf ("%d\n", B); Swap1 (&a,&b); printf ("%p\t%p", A, b); return 0;}
Here we first define two integer variables, a, b
Then define a temporary pointer variable to store the intermediate variable
Then assign the memory address of variable B to t, note that T is the memory address
Then give the value of B to a, and then B to accept the variables stored in the T address, note that the *t here refers to the variable referred to in the address of T.
Pointers and Arrays
In fact the array of int a[10];
A is actually a pointer to the a[0] of the first element of the array.
So the array variable itself is the expression address, so
1 inta[Ten];2 int*p=a;//no need to take & address3 4But the elements of the array are the variables that need to be used &Fetch Address5a==&a[0];6 7 The [] operator can do the array, or it can do the pointer:8 9 Tenp[0] <==>a[0]; OneHere's p[0Equivalent toP A -*a can represent a[0] - theThe array variable is a const pointer
The function of the const modifier is to indicate that a variable is a specified value that cannot be changed.
int Const // p is a const*q=; // OK // ERROR
1 intls= -;2 intls1= -;3 int*Constq=&ls;4*q =Ten;5*q= -;6 //q = &ls1; This sentence cannot be compiled because the pointer is not an address that can point to another location7 //execution success indicates that the pointer variable here can be changed8ls= $ ;9printf"\n%d",*q);Ten //and then we'll look at another situation . One intp1=Ten; A intP2= -; - int Const*t=&P1; -t=&p2;//execution result is the //*t = 15; Here is not compiled, because the pointer to the location of the stored variables can not be assigned to change the value. -P2= -; -printf"\n%d",*T); - //The key is that the const here is in front of the * or behind the decision + return 0;
1 void mai (constint*p) {2 3 int *ls=* 4 printf ("LS's address:%p", ls); 5 6 Here we pass in a pointer, but in a function, you cannot change the value that the pointer points to.
Operation of Pointers
1#include <stdio.h>2#include <stdlib.h>3 4 intMain () {5 inti,j;6I=Ten; j= A;7printf"i,j:%d,%d\n",&i,&j);8printf"i,j:%d,%d", *&i+1,&j);9printf"\n&i-&j:%d", (&i)-(&j));//Here are two address differences are actually the address difference/type occupies the number of digitsTen One int*p; Ap=&i; -*p++;//is actually * (p++); + + priority is higher than *; array traversal -printf"\n*p:%d",*p); the return 0; -}
Type conversions for pointers
void* A pointer to something that doesn't know what it's pointing at.
The size of the pointer variable is the same, but the pointer is of a different type
In short, the role of pointers
1 and need to pass in large data when used as parameters 2 , array operation after passing in 3 , the function returns more than one time can be carried out with the pointer (C can only return a variable, or value) Need to use function to modify more than one variable 4, dynamic request memory ....
The specific follow-up will be more.
A simple example of a C language pointer