当前位置:网站首页>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 .
边栏推荐
- Enable shallow and deep copies+
- Wechat applet personal number opens traffic master
- DRF use: get request to get data (small example)
- 论文缩小
- The difference and use between get request and post request
- Crmeb Pro v1.4 makes the user experience more brilliant!
- JSP Foundation
- It can carry 100 people! Musk releases the strongest "starship" in history! Go to Mars as early as next year!
- Leetcode 226 翻转二叉树(递归)
- SolidWorks simulation curve attribute setting
猜你喜欢

Pychart imports the existing local installation package

centos上mysql5.7主从热备设置

The difference and use between get request and post request

Your password does not satisfy the current policy requirements (modify MySQL password policy setting simple password)

SolidWorks simulation curve attribute setting

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

Security software related to wireless network analysis (airtrack ng)

DRF use: get request to get data (small example)

JMeter5.3 及以后的版本jmeter函数助手生成的字符在置灰无法复制

For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!
随机推荐
Time series - use tsfresh for classification tasks
DRF learning notes (II): Data deserialization
嵌入式面试
Content ambiguity occurs when using transform:translate()
Vant UI toast and dialog use
Determine the exact type of data
C language realizes the conversion between byte stream and hexadecimal string
Have you ever used the comma operator?
firefox旧版本
JWT简介
减小PDF文档大小(转载)
Security software related to wireless network analysis (airtrack ng)
The solution to the memory exhaustion problem when PHP circulates a large amount of data
这些题~~
The 4.3 billion euro cash acquisition of OSRAM failed! AMS said it would continue to acquire
Multiline text overflow dotting
The new JMeter function assistant is not under the options menu - in the toolbar
centos yum方式安装mysql
Example of the task submitted by the Flink packer
The difference and use between get request and post request