1: Introduce pointer
Variable name - > address - > Data
Function internal change, function external unchanged: (pass by value)
#include<stdio.h> void swap(int a,int b)//Pass by address { int temp=0; temp=a; a=b; b=temp; } int main() { int a=3,b=4; printf("Before exchange a=%d,b=%d\n",a,b);//a=3,b=4 swap(a,b); printf("After exchange a=%d,b=%d\n",a,b);//a=3,b=4 return 0; }
2: Pointer - pass by address
Pointer definition: pointer is the data type used to describe the address
The following a is a pointer variable of type int
#include<stdio.h> void swap(int *a,int *b)//Pass by address { int temp=0; temp=*a; //Address value *a=*b; *b=temp; } int main() { int a=3,b=4; printf("Before exchange a=%d,b=%d\n",a,b);//a=3,b=4 swap(&a,&b); //Address passed in printf("After exchange a=%d,b=%d\n",a,b);//a=4,b=3 return 0; }
3: Pointer variable: store the address of others
As follows: pointer variable p stores the address of variable a
#include<stdio.h> void swap(int *a,int *b)//Pass by address { int temp=0; temp=*a; //Address value *a=*b; *b=temp; } int main() { int a=1; //Pointer variable (store someone else's address) int *p=&a;//Definition and initialization of pointer variables printf("a Your address is:%p\n",&a); //0019FF2C printf("Pointer variable p Your address is:%p\n",p);//0019FF2C (store others' addresses) return 0; }
4: Pointer variable initialization
The pointer variable is initialized to NULL and 0 is cast to void*
#include<stdio.h> int main() { //Pointer variable storage address int *p=NULL;//Definition and initialization of pointer variables printf("Pointer variable p Your address is:%p\n",p);//00000000 return 0; }
5: Pointer variables need to be initialized, otherwise the pointer will be null (dangerous operation)
#include<stdio.h> int main() { int a=3,b=4; //The wild pointer program ended abnormally int *pa; //Wild pointer int *p=NULL;//Pointer variable initialization NULL printf("Pointer variable pa Your address is:%p\n",pa);//CCCCCCCC return 0; }
Therefore, be sure to initialize NULL
6: Two ways of pointer definition
1. Define and assign initial value
#include<stdio.h> int main() { int a=3,b=4; int *p=NULL;//Definition 1 define and assign initial value p=&a; printf("Pointer variable p Your address is:%p\n",p);//0019FF2C printf("Pointer variable p The values are:%d\n",*p);//3 * value return 0; }
2. Definition and assignment
#include<stdio.h> int main() { int a=3,b=4; int *pb=&b; //Definition 2 definition assignment printf("Pointer variable pb Your address is:%p\n",pb);//0019FF28 printf("Pointer variable pb The values are:%d\n",*pb);//4 return 0; }
7: Pointers and pointer variables
The address of a variable is called the "pointer" of the change amount. The pointer is the address, which is used to describe the address
int * ptr / / ptr is a pointer variable of type int
Pointer variable: the address specially used to store another variable
Pointer variables are also variables. Pointer variables store other people's addresses, and * represents the value
Variables have addresses, so pointer variables also have addresses
#include<stdio.h> int main() { int a=3,b=4; int *p=NULL;//Definition 1 define and assign initial value p=&a; printf("%p\n",&p);//0019FF24 pointer variable is a variable, and the variable has an address (the address of p) printf("%p\n",*&p);//0019FF2C pointer variable value also uses * to get the value is an address (the address of a). Pointer variable stores the address of others printf("Pointer variable p Your address is:%p\n",p);//0019FF2C pointer variable stores the address of others (the address of a) printf("Pointer variable p The values are:%d\n",*p);//The value of 3 * is the value of a return 0; }
8: Pointer variable open space
1 byte 8 bits, 32-bit operating system 32-bit pointer variable open space is 4 bytes, which is independent of the base type and related to the stored content
#include<stdio.h> int main() { int a=3,b=4; int *p=NULL;//Definition 1 define and assign initial value char *pt=NULL; p=&a; //Size of pointer variable open space printf("%d\n",sizeof(p)); // 4 int pointer printf("%d\n",sizeof(pt)); // 4 char pointer return 0; }
9: void*
Use of void:
1. Function type
2. The void * type pointer represents the address of the object, but there is no information about the address of the object
void* k;//0 cast to void * is similar to NULL
Pointer benefits: no local, no extinction, used for function data transfer and parameter transfer. Passing by address, the function changes internally and externally.
&Get address -% p
*Value -% d
Only variables can get addresses, and values cannot get addresses. Cannot: & * P
*&Yes, you can get the value only after you have the address
*&A = = a first take the address of a and then the value of A. This formula is correct.
10: The pointer is an address and can be offset by the pointer
*p + +: first offset the address with p + +, then take the value of *, and the address changes to 28 - > 2C offset by 4 bits (int type pointer offset by 4 bits)
The char type pointer is offset by 1 bit, that is, the pointer offset is determined according to the basic data type.
(* p) + + is the first value and then increases by 1