当前位置:网站首页>Pointer summary
Pointer summary
2022-07-27 16:23:00 【Xavier】
Interesting pointer
c The problem of pointers in language is very interesting , Today, I'll make a simple summary of the pointer class I've seen .
1. The definition and use of pointers :
c Pointer in language is a variable specially used to store memory address , Each pointer has a data type associated with it , The general form we define
char *ch;
int *a;
double *pd = NULL;
Each pointer variable stores the address of memory
int a = 1;
int *p = &a;
2. Pointers and arrays
(1) stay c in , The array name is actually a pointer to the first element of the array
int a[] = {
2,4,6,8,10};
int *p = a;// Address assignment method 1 of the first element of the array
int *q = &a[0];// Address assignment method 2 of the first element of the array
(2) Of course, there are pointer arrays and secondary pointers
int a[5] = {
1,3,5,7,9};
int *b[5],i;
int **c = b;
So because of “[ ]” Has a higher priority than “*” , So at the moment *b[5] Equivalent to *(b[5]).
for(i = 0;i < 5;i++)
b[i] = &a[i];
Or maybe
int *p = a;
for(i = 0;i < 5;i++)
b[i] = p++;
At this time, the a The addresses of the elements of this array are assigned to b Array
Next is the secondary pointer , That is, the address where the memory address is stored ( It's like dolls ).
int **c = b;
Here will be b The address of the first element of is assigned to c .
for(i = 0;i < 5;i++,c++)
printf("%d ",**c);
Then output the data .
(3) Pointer array
int q[2][3];
First of all, we know that one dimension of a two-dimensional array is the address where two dimensions are stored , Then we can define :
int *p[2];
Due to priority ,p The first and “[ ]” Union and recombination “*” combination , So it's a pointer to an array . It can be understood according to the assignment relationship of relatively equal quantities
for(int i = 0;i < 2;i++){
p[i] = q[i];
}
(4) Array pointer
It is generally defined as :
int (*p)[4];
For its understanding, I think it is very similar to two-dimensional array ,p The first address of four consecutive one-dimensional arrays is stored in memory .
int a[2][3]={
1,2,3,4,5,6};
int (*p)[3];
p = a;
p++;//a[1][];
for(int i=0;i<3;i++){
printf("%d ",(*p)[i]);
}
The result is 4 5 6
3. Pointers and functions :
(1) Pointer type variables can also be used as parameters of functions
void func(int *a,int *b){
...
}
(2) The most interesting thing is that pointers can also point to functions , Call it a function pointer .
Generally defined as :
int (*p)(int,int);
For example, the following example :
int get_max(int i,int j){
return i>j?i:j;
}
int get_min(int i,int j){
return i > j?j : i;
}
int compare(int i,int j,int flag){
int ret;
int (*p)(int,int);// Pointer to function , The return value of this function is int type
if(flag == GET_MAX)
p = get_max;// Assignment of function name
else
p = get_min;// Assignment of function name
ret = p(i,j);// Receive return value
return ret;
}
And here we need to distinguish
int *p(int i,int j);// Functions that return pointers
int (*p)(int i,int j);// Defines a pointer to a function
(3) Function pointer as formal parameter
int get_big(int i,int j){
return i > j?i : j;
}
int get_max(int i,int j,int k,int (*p))(int,int)){
int ret;
ret = p(i,j);
ret = p(ret,k);
return ret;
}
int main(void){
int i = 5,j = 10,k = 15,ret;
ret = get_max(i,j,k,get_big);// Pass this function as an argument
printf("%d",ret);
return 0;
}
4. Strings and pointers
And array pointer
char ch[] = "hello world";
printf("%s",ch);
char *p = "hello world";// Define a string pointer
printf("%s",p);
The pointer of the string can be used to copy the string array , You can use this example to understand .
char a[] = "hello world";
char b[20],c[20];
char *p1 = a,*p2 = c;
for(int i=0;i<sizeof(a);i++)
b[i]=a[i];
while(*p1 != '\0') *(p2++)=*(p1++);
printf("%s\n",a);
printf("%s\n",b);
printf("%s\n",c);
Of course, the results are the same
The above is my summary of pointer , Thank you for browsing , I hope I can put forward valuable suggestions .
边栏推荐
- Openwrt adds support for SD card
- google chrome revercecaptcha广告屏蔽
- DRF learning notes (IV): DRF view
- profileapi. h header
- Samsung closes its last mobile phone factory in China
- JSP基础
- What is ti's calculation for successively canceling the agency rights of anfuli / Wenye / Shiping?
- Firefox old version
- 减小PDF文档大小(转载)
- webRTC中的coturn服务安装
猜你喜欢

web测试学习笔记01

JSP基础

微信小程序个人号开通流量主

Scratch crawler framework

Penetration test - dry goods | 80 + network security interview experience post (interview)

C channel simply implements the publishing and subscription of message queue

The difference and use between get request and post request

MapReduce instance (III): data De duplication

使用transform:translate()出现内容模糊问题

COMS Technology
随机推荐
Example of the task submitted by the Flink packer
Boolean value
嵌入式面试
Servlet basic knowledge points
centos yum方式安装mysql
Draw circuit diagram according to Verilog code
ADAMS中转动整个模型
Characters generated by JMeter function assistant in jmeter5.3 and later versions cannot be copied when they are grayed out
重新配置cubemx后,生成的代码用IAR打开不成功
Leetcode 226 翻转二叉树(递归)
将IAR工程文件夹转移目录后无法进入函数定义解决
Busybox login: can't execute'/bin/bash': no such file or directory solution
Solve mt7620 continuous cycle uboot (LZMA error 1 - must reset board to recover)
论文缩小
Security software related to wireless network analysis (airtrack ng)
Determine the exact type of data
The new JMeter function assistant is not under the options menu - in the toolbar
matlab legend用法
第一章 马克思主义哲学是科学的世界观和方法论
Live broadcast software development, customized pop-up effect of tools