当前位置:网站首页>Pointer learning of C language -- the consolidation of pointer knowledge and the relationship with functions, arrays and structures
Pointer learning of C language -- the consolidation of pointer knowledge and the relationship with functions, arrays and structures
2022-07-28 19:14:00 【Learn something】
Read a tutorial , Further study C The pointer to language , The reference tutorial has no source ( Invasion and deletion )
Start with the name , Combine according to the priority of symbols
name :
“ Integer variables ” —— int a; such
“( Point to ) Integer pointer ”—— int *p; such , Point to integer variable
“ integer array ”——int a[5]; such , Array elements are all integer variables
Priority of symbol combination :
Represents an array [ ] > Indicates that it is a pointer * ;
Indicates that it is a function ()> Indicates that it is a pointer *;
1. int p;
Defines an integer variable p
2. int *p;
Start with the name , The name is p;
The first and * combination —— Express p Is a pointer ;
And again int combination —— Indicates that the pointer points to an integer .
**p Is a pointer to an integer variable, that is “ Integer pointer ”**
3. int p[5];
Start with the name , The name is p;
The first and [] combination —— p It's an array ;
And again int combination —— Array elements are integers .
**p Is an array of integer variables ( integer array )**
4. int *p[2];
Start with the name , The name is p;
The first and [] combination —— p Is an array ;
And again * combination —— Array elements are pointer types ;
And again int combination —— The pointer points to an integer ( Integer pointer );
**p It's a by ( Point to ) integer ( Of ) An array of pointer variables **
5. int (*p)[2];
By name , The name is p;
The first and * combination —— p It's a pointer ;
And again [] combination —— Pointer to array ;
And again int combination —— Array elements are all integers ;
**p Is a pointer to an integer array **
6. int **p;
By name , The name is p;
First with one * combination —— p It's a pointer ;
And another * combination —— The pointer points to the pointer ;
And again int combination —— The pointer pointed to is a pointer to an integer ( Integer pointer );
**p Is a pointer to an integer pointer ( The secondary pointer )**
7. int p(int);
By name , The name is p;
First, sum with (int) combination —— p It's a function ;
Get into () yes int —— The function needs a int Type parameter ;
And then with the outside int combination —— The return value of the function is an integer ;
p It's a “ Integer is required as a parameter , The return value is an integer ” Function of
8. int (*p)(int);
By name , The name is p;
The first and * combination —— p It's a pointer ;
And then sum the (int) combination —— Pointer to function ;
Get into () yes int —— The function needs a int Type parameter ;
And then with the outside int combination —— The return value of the function is an integer ;
p It's a point “ Integer is required as a parameter , The return value is a function of integer quantity ” The pointer to
9. int *p(int);
By name , The name is p;
The first and (int) combination —— p It's a function ;
And again * combination —— The return value of the function is a pointer ;
And again int combination —— The pointer points to an integer ;
**p It's a “ Integer is required as a parameter , The return value is a pointer to an integer ” Function of **
10. int p(int)[2];
By name , The name is p;
The first and (int) combination —— p It's a function ;
And again [] combination —— The return value of the function is an array ;
And again int combination —— Array elements are integers ;
p It's a “ The parameter is an integer , The return value is an integer array ” Function of
11. int (*p(int))[2];
By name , The name is p;
The first and (int) combination —— p It's a function ;
And again * combination —— The return value of the function is a pointer ;
And again [] combination —— Pointer to array ;
And then with the outside int combination —— Array elements are integers ;
**p It's a “ Integer is required as a parameter , The return value is a pointer to an integer array ” Function of **
12. int *(*p)[2];
By name , The name is p;
The first and * combination ——p It's a pointer ;
And again [2] combination —— Pointer to array ;
And again * combination —— Array elements are pointer types ;
And again int combination —— The pointer points to an integer ( That is, array elements are integer pointers );
p It's a point “ It consists of integer pointer variables Array of ” The pointer to
12. int *(*p(int))[2];
By name , The name is p;
The first and (int) combination —— p It's a function ;
And again * combination —— The return value of the function is a pointer ;
And again [] combination —— Pointer to array ;
And outside * combination —— Array elements are pointer variables ;
And then to the outside int combination —— Pointer to integer variable ;
p It's a “ The parameter is an integer , The return value points to An array of integer pointer variables The pointer to ” Function of
```c
Pointer and memory :
The value of the pointer itself —— It's the address , Represents the memory area ;
The value that the pointer points to —— This is the address ( Memory area ) Data stored on ;
The memory area pointed to by the pointer starts from the memory address represented by the pointer , The length is sizeof( The type that the pointer points to ) An area of .
We say that a pointer value is XX, It is equivalent to saying that the pointer points to XX An area of memory with the first address ;
We say that a pointer points to a certain area , It is equivalent to saying that the value of this pointer is the first address of this memory area .
The memory area occupied by the pointer itself :
stay 32 In the platform , The pointer itself occupies 4 Bytes
The arithmetic operation of the pointer :
The pointer can add or subtract an integer —— Is the addition and subtraction in units
Two pointers cannot be added ;
Two pointers can be subtracted , But it must be of the same type ( Generally used in arrays );
char a[20]="You_are_a_girl";
char *p=a;
char **ptr=&p;
printf("**ptr=c%\n",**ptr);
ptr++;
printf("**ptr=c%\n",**ptr);
analysis :
p It's pointing char The pointer to ; ptr It's a secondary pointer , It points to a pointer ;
When p+1 when , The memory address moves size(char)=1 Byte position ,
Just as soon as it arrives a[1] On the address of ; When ptr+1 when , The memory address moves size(p)=4 Byte position ,
however ! This is a ptr Moved four bytes , Who knows where it moved ( If so p Move 4 Bytes
The location of , That's it a[3] Location. ), So , In fact, it ran away ——
The secondary pointer moves easily
summary :
A pointer ptrold Add ( subtract ) An integer n after , The result is a new pointer ptrnew,ptrnew The type and ptrold Same type of ,ptrnew The type and ptrold It's the same type .ptrnew The value of will be higher than ptrold The value of the increase ( Reduce ) n * sizeof(ptrold The type pointed to ) Bytes !!!—— Moved n A space of the type he points to —— Move and access like an array
operation :*、&
*p The result is that p What it points to :
His type is p Type of point , The address he occupies is p The address pointed to
int a=12,b;// integer
int *p;// Pointer to the variable
int **ptr;// The secondary pointer
p=&a;//p It's a pointer variable , Take out a The address of is then assigned to p
*p=24;//*p Express p The value of the point ——a, Is equivalent to a The assignment is 24
ptr=&p;// On the left is the secondary pointer , On the right is the address of the pointer
*ptr=&b;
//*ptr Express ptr Type of point —— That is, the first level pointer p, On the left is the first level pointer , On the right is the primary address
**ptr=34;
//**ptr: The secondary pointer takes the address twice and becomes the corresponding data type ——int type , On the right is an integer 34
Arrays and pointers
The array name of an array can be regarded as a pointer
char *str[3]={
"hello", "goodmorning", "world"};
//str With the first [] combination —— It's an array ; And again * combination —— Array elements are pointer types ;
// And again char combination —— yes char Type pointer ;str It's a “ from char Type pointer ” Array
// The string is equivalent to an array , Store in memory as an array , It's just that the string is a
// Array constants , The content cannot be changed . If you look at it as a pointer , It is a constant pointer , It's also a pointer constant
// Array elements are pointers , Then the array name will obviously become a secondary pointer —— Pointer
str[0]=*str;//str It's a secondary pointer ,*str Express str The pointer pointed to
str[1]=*(str+1);//1 What moves is 1 individual char* The length of the type —— Move 1 Types indicated
str[2]=*(str+2);//2 What moves is 2 individual char* The length of the type —— Move 2 Types indicated
str+1 It's a secondary pointer , Point to the... Of the array 1 Unit No , namely "goodmorning" The address of
*(str+1) Is a first level pointer , Point to "goodmorning" First character of "g"
边栏推荐
- Pytorch GPU yolov5 reports an error
- Overview and working principle of single chip microcomputer crystal oscillator
- CTR click through rate prediction practice project of advertising recommendation!
- Regular expressions related to face-to-face orders of major express companies in JS
- 2022 Niuke multi School Game 2 J. link with arithmetic progress (three points + enumeration)
- MES生产管理系统对设备的应用价值
- Is software testing really as good as online?
- Three minutes to understand, come to new media
- Introduction and advanced MySQL (7)
- Is it useful to learn software testing?
猜你喜欢

The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn

From Bayesian filter to Kalman filter (I)

Win11电脑摄像头打开看不见,显示黑屏如何解决?

OAI L3 and L2 interface analysis

How to solve the problem that the win11 computer camera cannot be seen when it is turned on and the display screen is black?

What does real HTAP mean to users and developers?

软件测试开发基础|测开中的几个工具开发实战

Getting started with gateway
![[R language - basic drawing]](/img/1e/aebf1cbe02c4574671bac6dc2c9171.png)
[R language - basic drawing]

ACM warm-up exercise 3 in 2022 summer vacation (detailed)
随机推荐
Qt: one signal binds multiple slots
现代化个人博客系统 ModStartBlog v5.4.0 登录界面改版,新增联系方式
What if svchost.exe of win11 system has been downloading?
How to break through the bottleneck of professional development for software testing engineers
Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?
3、 Uni app fixed or direct to a certain page
关于白盒测试,这些技巧你得游刃有余~
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
Is zero basic software testing training reliable?
Creating new projects and adding your own programs
The difference between --save Dev and --save in NPM
GC garbage collector details
【雷达】基于核聚类实现雷达信号在线分选附matlab代码
Swiftui swift forward geocoding and reverse geocoding (tutorial with source code)
软件测试开发基础|测开中的几个工具开发实战
Xiaobai must see the development route of software testing
2022年最火的十大测试工具,你掌握了几个
The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn
How to use the white list function of the video fusion cloud service easycvr platform?
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held