当前位置:网站首页>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 .
边栏推荐
- Mysql5.7 master-slave hot standby settings on CentOS
- Install MySQL using CentOS yum
- Servlet基础知识点
- Axure install Icon Font Catalog
- How PHP changes a two-dimensional array into a one-dimensional array
- C language realizes the conversion between byte stream and hexadecimal string
- Crmeb Pro v1.4 makes the user experience more brilliant!
- ARIMA模型选择与残差
- 微信小程序个人号开通流量主
- 4-digit random data
猜你喜欢

flume增量采集mysql数据到kafka

Boolean value

web测试学习笔记01

matlab legend用法

Nacos

Draw circuit diagram according to Verilog code

Pychart imports the existing local installation package

Wechat applet personal number opens traffic master

Solve mt7620 continuous cycle uboot (LZMA error 1 - must reset board to recover)

Openwrt adds RTC (mcp7940 I2C bus) drive details
随机推荐
Have you ever used the comma operator?
Chuanyin holdings disclosed that it was prosecuted by Huawei: a case has been filed, involving an amount of 20million yuan
Install MySQL using CentOS yum
Simulation生成报表
嵌入式面试
Busybox login: can't execute'/bin/bash': no such file or directory solution
Nacos
const小结
SolidWorks simulation curve attribute setting
Test novice learning classic (with ideas)
Text capture picture (Wallpaper of Nezha's demon child coming to the world)
Chapter I Marxist philosophy is a scientific world outlook and methodology
Personal perception of project optimization
DRF learning notes (preparation)
TP5 paging some small points
word中插入度的方法
COMS Technology
Wechat applet personal number opens traffic master
profileapi. h header
Leetcode234 question - simple method to judge palindrome linked list