当前位置:网站首页>Sorting out the knowledge points of primary and secondary indicators
Sorting out the knowledge points of primary and secondary indicators
2022-06-12 21:05:00 【Bald and weak】
Normal pointer ( Class A ):
Definition :
The pointer = Address
Fetch address :&+ Variable name ( Get the address of the variable ) , in the light of All variables All applicable
Output address :printf(“%d”,&a); //%d Output 10 Base number ,%p Output 16 Base number
Add :scanf in scanf("%d",&a); use & The same goes for input values , Only through the address can scanf Bring the value from the formal parameter in the function
Use :
(1) Change the value of a variable by dereference :
*+ Pointer to the variable : Quoting , Indirect accessor
for example :int* p=&a;
*p=100;

p Point to a,p In the grid a The address of .
*p It's right a Quoting , obtain a Values in the grid , That is to say a Value , Make *p=100, be a=100
Similarities and differences between pointer variables and ordinary variables :
The same thing :
- Can change
- They all have addresses
Difference :
- Pointer variables can be dereferenced , Ordinary variables cannot
summary :
int p;// Defining integer variables
int* p; // Define integer address variables
&a; // Shaping address variables
p=&a;// Shaping address variables p Save shaping a Address values
int*p=&a;// Combine the above two steps
Pointer byte size :
It's not about type , Determined by the platform
X86:32 Bit operating system ;2^32 A byte 8 position , In this case , The pointer 4 Bytes
x64:64 Bit operating system 2^68 In this case , The pointer 8 Bytes
One byte =-128 To 127 256 2^8
1TB=1024GB;
1GB=1024MB;
1MB=1024KB;
1KB=1024B;
1B=8b;
Add :
(1)Int*p1,p2,p2;
p1: Pointer to the variable ,p2,p3: Integer variables
(2)typedef int* INT;
INT p1,p2,p3; // here , All three are pointer variables , Equivalent to Int*p1,*p2,*p2;
(3)#define INT int*
INT p1,p2,p3; // here , Only the first is a pointer variable , Equivalent to Int*p1,p2,p2;
Exercises :
eg1:
// Knowledge point : Pointer types must be strictly equal to assign values ( Is the type of variable , What type of pointer )
char a;
short b;
int c;
unsigned long d;
double e;
float f;
(1) Define pointer p1, preservation a The address of
char* p1 = &a;
(2) Define pointer p2, preservation b The address of
short* p2 = &b;
(3) Define pointer p3, preservation c The address of
int* p2 = &c;
(4) Define pointer p4, preservation d The address of
unsigned long* p2 = &d;
(5) Define pointer p5, preservation e The address of
double* p5 = &e;
(6) Define pointer p6, preservation f The address of
float* p6 = &f;
eg2:
int a, b, c;
int* p1 = &a;
int* p2 = &b;
int* p3 = &c;
(1) adopt p1, take a To change the value of 20, And the output ;
*p1 = 20;
printf("%d", *p1);
(2) adopt p2, take b To change the value of 30, And the output ;
*p2 = 30;
printf("%d", *p2);
(3) adopt p1,p2,p3, take c To change the value of a×b, And the output ;
*p3 = *p2 * *p2;
printf("%d", *p3);
(4) Define pointer p, Point to a
int* p = &a;
(5) adopt p1, take a To change the value of 10;
*p1=10;
(6) Define pointer p, take p Point to b, And pass p take b To change the value of 20
int*p = &b;
*p = 20;
(8) Define pointer p, adopt p Output a,b Value ( Need more than one statement )
int*p;
p = &a; printf("%d", *p);// Output a The address of ;
p = &b; printf("%d", *p);// Output b The address of ;
The benefits of pointers :
(1) General variables want to bring back values , Only use return Bring back a value , and The pointer can bring back multiple values through formal parameters .
example :
void MyScanf(int* a, int* b)
{
*a = 1;
*b = 0;
}
int main()
{
int a ;
int b ;
MyScanf(& a, & b);
return 0;
}
example :
int Operate(int a,int b,int* sum,int* mul)
{
*sum=a+b;// Pass the address in the main function , The function internally dereferences the replacement value
*mul=a*b;
}
int main()
{
int sum,mul;
Operate(1,2,&sum,& mul);
}
(2) because The address of the formal parameter is different from that of the actual parameter , Parameter is a local variable , The argument value cannot be modified directly through formal parameters , Address change required , and A pointer can change its value by calling a function .
example :
// Exchange variables
void Swap1(int *a,int* b)
{
int *temp=a;
a = b;
b = temp;
}
void Swap2(int *a,int* b)
{
int temp=*a;
*a = *b;
*b = temp;
}
// as follows Swap function error : Dereference the wild pointer
void Swap(int *a,int* b)
{
int *temp=*a;
*a = *b;
*b = *temp;
}
The secondary pointer
Illustrate with examples :
example :int a=10;
int *p=&a; //p Point to a,(p Inside the grid ) preservation a The address of
int **pp=&p; //pp Point to p,(pp Inside the grid ) preservation p The address of

“*”: Quoting ( The grid arrow points forward )
pp:pp lattice =200;
*pp:p Value =100 //1 individual “*”, Arrow goes forward 1 Next .*pp It's right p Dereference to get p Values in the grid , That is to say 100;
**pp:a Value =10 //2 individual “*”, Arrow goes forward 2 Next .**pp It's right a Dereference to get a Values in the grid , That is to say 10;
usage :
change pp The direction of , Make it point to q:pp=&q;
Through the secondary pointer pp modify a Value :**pp=200;
summary :
The first level pointer associates the address of the variable ,
The second level pointer is associated with the address of the first level pointer .
Add :
&&a error
*&a=a; Right , Yes a Take the address and dereference it
&*a error , Not right a Quoting
Example analysis
eg1:
int a = 10;
int b = 20;
int* p;
p = &a;
*p = 30;
int** pp = &p;
(1)a, and **p What is the value of ?
a=30; //p Point to a,*p = 30 Revised a Value ;
**pp = 30;// Solve two references , The arrow jumps twice to point to a,a The value in the grid is 30

eg2:
int a = 10;
int b = 20;
int* p= &a;
*p=100;
int** pp = &p;
*pp = &b;
**pp =1000;
int ***ppp =&pp;
***ppp=1;
(2)a and b What is the value of ?
*p=100; take a To change the value of 100
*pp = &b; take p The direction of is changed to b
**pp =1000; Explain 2 Times quoted , take b Is changed to 1000
int ***ppp =&pp; Definition ppp Point to pp
***ppp=1; take b Is changed to 1
therefore ,a=100,b=1

Add : Understanding of wild pointer
int *p=&a
printf(“%d”,*p); // Sure
however
p=NULL;
printf(“%d”,*p);// no way , Null cannot be dereferenced
however , As follows :
Definition Fun(p) function , On the inside p empty , call Fun(p), Then the output printf(“%d”,*p)
void Fun(int* p)
{
p = NULL;
}
int main()
{
int a = 10;
int* p = &a;
Fun(p);
printf("d\n", *p);
return 0;
}
// At this time, we found that Fun Function doesn't work , because :A Function call B function , If it needs to be modified A The value of the argument in , Then the pointer must be passed , stay B Re dereference in
//fun Function not executed , Want to change its value through another , The pointer must be passed , Function internal dereference ( Add *)
// As shown below , Can cause errors
void Fun(int** p)
{
**p = NULL;
}
summary :
NULL Null pointer , Currently invalid pointer , It is equivalent to telling the user that the pointer is invalid
Wild pointer : No access rights , Pointer definition not initialized , Easy to generate wild pointer
边栏推荐
- Research Report on hydraulic injection machine industry - market status analysis and development prospect forecast
- Successful transition from self-study test halfway, 10K for the first test
- MinIO客户端(mc命令)实现数据迁移
- 循环插入excel某一列,以及多列之和
- #981 Time Based Key-Value Store
- torch. unique()
- 同花顺能开户吗,在同花顺开户安全么
- At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?
- What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
- What did new do
猜你喜欢

Solve the cvxpy error the solver GLPK_ MI is not installed

JSP中的监听器

Nexus3搭建本地仓库

机器学习资料汇总

Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"

shell语言

GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28

Composer version degradation

Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?

EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
随机推荐
How to determine the sample size of an inspection lot in SAP QM's initial sampling strategy?
插入排序
shell语言
JSON file handles object Tags
金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
Preliminary understanding of regular expressions (regex)
New product release Junda intelligent integrated environmental monitoring terminal
Pytoch distributed training error
竣达技术丨适用于“科士达”智能精密空调网络监控
Lintcode:127. Topology sorting
#886 Possible Bipartition
作用域和作用域链
How can CTCM in the inspection lot system status of SAP QM be eliminated?
In the spring recruitment of 2022, the test engineer will have a full set of interview strategies to thoroughly understand all the technical stacks (all dry goods)
Large and small end conversion
求解一维数组前缀和
Nexus3搭建本地仓库
Restful API interface specification
Gather function in pytorch_
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital