A simple word, the pointer is the address
0: Online shopping address, through this address to find you,
The address of the
program, through the address to manipulate variables, this address has a name called pointer, Java inside the address is called reference
1: The unit of memory is a byte, for example a 256MB machine, with 256*1024*1024 bytes of storage units, each byte has a corresponding address
The
block memory can be found by this address, just like our mailing address
2: Each variable consists of several bytes, for example: int age = 20 is made up of 4 bytes, the address of the first byte in the variable is called the address of the variable
#include <stdio.h>int main (int argc,const char *argv[]) {int = 20;printf ("size=%lu\n", sizeof (age));p rintf ("%p \ n ", &age);//This is the first address of the output age//1: Every byte in memory has an address//2: The address of the variable is the address of the first byte (the first address), the address indicates the position of the variable in memory, the address is the pointer// In C language, as long as the pointer refers to the address//3: Address, pointer is constant, pointer variable = (pointer variable)//4: Pointer is a type, is a pointer type, the address in Java is called reference, type is called reference type return 0;}
2: We refer to the variable's first byte address as a pointer to the variable
to go to the human pointer and pointer variable:
The pointer is an address. A pointer variable is a variable that holds an address, first it is a variable, but it is a saved address.
3:
& Take Addressoperator, take the address of the variable
* Indirect access operator to access the value at the specified position
Variable declaration when the variable is a pointer, use the pointer variable when preceded by this *, to identify the value of the position
<span style= "White-space:pre" ></span>int a = 100;<span style= "White-space:pre" ></span>int *p =&a;<span style= "White-space:pre" ></span>//*p is the alias of a <span style= "White-space:pre" ></span The change of >//to *p is the change of a//&,*//& take the address operator int ii=100;printf ("%p\n", &ii);//&i three gold ingot where? Address int *pp =?printf ("%p\n", pp);//Here is%pprintf ("%d\n", *pp);//*p Three gold ingot, note here is%d*pp=80;printf ("%d\n", ii); II=200;PRINTF ( "%d\n", *pp);
4: Pointer not pointing to any variable, called dangling pointer is actually an uninitialized pointer
The initialization of the pointer int *ip;//The dangling address, 0x0printf ("%p", IP), int b = 100;int *PB = &b;//initialization//pointer variable PB//PB points to B//*PB equivalent to B//*PB! = Pbint *p1;int j = 200;p1=&j;//Assignment
5: Assignment of pointers
Pointer assignment: Simply the copy of the address, int q = 100;int *PQ = &q;printf ("pg=%p\n", PQ), int *pqp = pq;printf ("pqp=%p", pqp); *PQ = 200;printf ( "q=%d\n", Q);p rintf ("pq=%d\n", *PQ);p rintf ("pqp=%d\n", *PQP);
6: Pointer as argument to function
Pointers as parameters of the function, in the C language, the default is to use the form of a copy to do, if you want to really switch the value of two variables, using swapaddressint AA = 1,BB = 2;swap (AA,BB);p rintf ("aa=%d,bb=%d", AA , BB); swapaddress (&AA,&BB);p rintf ("aa=%d,bb=%d", AA,BB);//Exchange two value void swap (int a, int b) {int temp =a;a = b;b=temp;} void swapaddress (int *a,int *b) {int temp =*a;*a = *b;*b=temp;}
Sometimes I don't want you to change my parameters, using constant, like final in Java
void Test (int *p) {*p = 1000;} void Testconst (const int *p) {//If the following error occurs when compiling assignment of read-only location ' *p '//*p = 1000;} Sometimes don't want to change it? Set the parameter to const similar to java inside final,int TESTP = 100;test (&TESTP);p rintf ("i=%d\n", TESTP), Testconst (&TESTP);p rintf ("I =%d\n ", TESTP);
Pointers can also be used as return values:
The pointer can also be used as the return value: int cc=100,dd=200;int *ee = max (&CC,&DD);p rintf ("Max is%d", *ee), int *max (int *a,int *b) {if (*a>* b) {return A;} Else{return b;}}
++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>//exchange two values void swap (int a, int b) {int temp =a;a = b;b=temp;} void swapaddress (int *a,int *b) {int temp =*a;*a = *b;*b=temp;} void Test (int *p) {*p = 1000;} int *max (int *a,int *b) {if (*a>*b) {return A;} Else{return b;}} void Testconst (const int *p) {//If the following error occurs when compiling assignment of read-only location ' *p '//*p = 1000;} int main (int argc,const char *argv[]) {int = 20;printf ("size=%lu\n", sizeof (age));p rintf ("%p\n", &age);// This is the first address of the output age.//1: Every byte in memory has address//2: The address of the variable is the address of the first byte (the first address), the address indicates the position of the variable in memory, the address is the pointer//c language inside as long as the pointer is referring to the address//3: Address, the pointer is Pointer variable = (pointer variable)//4: Pointer is of type, pointer type, Java address is called reference, type is called reference type//pointer variable declaration and point to//* representation, subsequent variable is pointer variable int i =100;int *p;//p is int type pointer, The pointer points to the type of the variable, the pointer holds the amount of the variable address of the type P =&i;//this sentence, also called P pointing to iprintf ("int type pointer +%p\n", p);//&,*//& take address operator int ii=100;printf ("%p\n", &ii);//&i where are the three gold ingots? Address int *pp =?printf ("%p\n", pp);//Here is%pprintf ("%d\n", *pp);//*p Three gold ingot, note here is%d*pp=80;printf ("%d\n", ii); II=200;PRINTF ( "%d\n", *pp);//The initialization of the pointer int *ip;//the dangling address printf ("%p", IP); int b = 100;int *PB = &b;//initialization//pointer variable PB//PB points to B//*PB equivalent to B//*PB! = Pbint *p1;int J = 200;p1=&j;//Assignment//pointer assignment: Simply the copy of the address int q = 100;int *PQ = &q;printf ("pg=%p\n", PQ); int *PQP = Pq;p rintf ("pqp=%p", pqp); *PQ = 200;printf ("q=%d\n", Q);p rintf ("pq=%d\n", *PQ);p rintf ("pqp=%d\n", *PQP);//pointers as arguments to functions, In the C language, the default is to use the form of a copy to do, if you want to really switch the value of two variables, using swapaddressint AA = 1,BB = 2;swap (AA,BB);p rintf ("aa=%d,bb=%d\n", AA,BB); Swapaddress (&AA,&BB);p rintf ("aa=%d,bb=%d\n", AA,BB);//sometimes do not want to change it? Set the parameter to const similar to java inside final,int TESTP = 100;test (&TESTP);p rintf ("i=%d\n", TESTP), Testconst (&TESTP);p rintf ("I =%d\n ", TESTP);//The pointer can also be used as the return value: int cc=100,dd=200;int *ee = max (&CC,&DD);p rintf (" max.%d ", *ee); return 0;}
C language pointer, a simple sentence pointer is the address