当前位置:网站首页>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
边栏推荐
- 金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
- (11) Image frequency domain filtering with OpenCV
- 同花顺能开户吗,在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
- 同花顺能开户吗,在同花顺开户安全么
- Library cache lock brought by add trandata
- Minio client (MC command) implements data migration
- Solution of multi machine room dynamic loop status network touch screen monitoring
- Cv2.lut() (populates the output array with values from the lookup table)
- 做自媒体视频,友好的新媒体运营必备app分享
- 服务端口不通排查
猜你喜欢

torch. nn. Linear() function

What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones

Solve the cvxpy error the solver GLPK_ MI is not installed

Image processing 12- image linear blending

Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer

#113 Path Sum II

中小型机房动力环境综合监控解决方案

Successful transition from self-study test halfway, 10K for the first test

Algorinote_2_主定理与 Akra-Bazzi 定理

Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
随机推荐
torch. unique()
SAP WM preliminary transaction code lx29 - list of fixed storage bins
服务端口不通排查
Algorinote_ 2_ Main theorem and Akra bazzi theorem
atoi超强解析
Simplest ALV template
Restful API interface specification
Market trend report, technical innovation and market forecast of hydraulic torque wrench in China
Design and practice of Hudi bucket index in byte skipping
(11) Image frequency domain filtering with OpenCV
#113 Path Sum II
remote: Support for password authentication was removed on August 13, 2021
typeScript的定义类型:不能将类型“Timeout”分配给类型“number”;
Introduction to scala basic grammar (III) various operators in Scala
初步了解认识正则表达式(Regex)
重排数列练习题
UVa11991 Easy Problem from Rujia Liu
test
JSON file handles object Tags
做自媒体视频,友好的新媒体运营必备app分享