当前位置:网站首页>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
边栏推荐
- To understand Devops, you must read these ten books!
- Circularly insert one excel column and the sum of multiple columns
- Solution of multi machine room dynamic loop status network touch screen monitoring
- HR SaaS unicorn is about to emerge. Will the employee experience be the next explosive point?
- 跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
- Large and small end conversion
- The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
- 机器学习资料汇总
- Lake shore PT-100 platinum resistance temperature sensor
- UVa11991 Easy Problem from Rujia Liu
猜你喜欢

torch. Finfo function

Vs2017 environmental issues

没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?

lintcode:127 · 拓扑排序

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

Solution of multi machine room dynamic loop status network touch screen monitoring

leetcode:207. Class Schedule Card

Integrated monitoring solution for power environment of small and medium-sized computer rooms
![Li Mu [practical machine learning] 1.4 data annotation](/img/e4/2593b1dec04476a9cc3b4af94dc189.jpg)
Li Mu [practical machine learning] 1.4 data annotation

Algorinote_ 2_ Main theorem and Akra bazzi theorem
随机推荐
#141 Linked List Cycle
leetcode:210. 課程錶 II
JS deep and shallow copy
跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
New product release Junda intelligent integrated environmental monitoring terminal
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
leetcode:207. 课程表
Delphi XE7的蓝牙 Bluetooth
nn. PReLU(planes)
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Let Google browser fofa plug-in come alive
Market trend report, technical innovation and market forecast of hydraulic chain hoist in China
Teambition 协作应用心得分享|社区征文
最简单ALV模板
Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
Foreign brands become a thing of the past? What are the key words of the TV industry in 2022?
Social metauniverse: start from redefining yourself
Introduction to the characteristics of balancer decentralized exchange market capitalization robot
Preliminary understanding of regular expressions (regex)