当前位置:网站首页>Advanced pointer of C language
Advanced pointer of C language
2022-07-27 16:55:00 【Cabbage 00】
Catalog
About the meaning of array code
Array parameters and pointer parameters
One dimensional array parameters
Two dimensional array parameters
First level pointer parameter transfer
The secondary pointer transmits parameters
A pointer to an array of function pointers
Initial order of pointer

Character pointer
char* In general use
#include <stdio.h>
void main() {
char* ps = "hello bit";// In essence, the address of the first character of the string is stored in ps The pointer
char arr[] = "hello bit";// Essentially, put the string into an array arr in , however arr The array name represents the address of the first element
printf("%c\n", *ps);//h
printf("%c\n", *arr);//h
// The address of the starting position can be printed , Because it prints in string form , So I will look for it from the back “\0”
printf("%s\n", arr);
printf("%s\n", ps);
}#include <stdio.h>
void main() {
char str1[] = "hello bit";// Array 1
char str2[] = "hello bit";// Array 2
*str1 = 'p';
printf("%s\n", str1);// You can change
// The following string is a constant string. Only one copy is saved in memory and cannot be changed
char* str3 = "hello bit";
char* str4 = "hello bit";
//*str3 = 'p';
//printf("%s\n", str3);// Non modifiable
printf("%p\n", str1);//0019F88C
printf("%p\n", str2);//0019F878
// The following addresses are different from the above two addresses , Separate a space , Is the constant string address
printf("%p\n", str3);//00257B30
printf("%p\n", str4);//00257B30
}Pointer array
Definition : An array of pointers

#include <stdio.h>
void main() {
int a = 10, b = 20, c = 30;
int* arr1[3] = { &a,&b,&c };// An array of plastic pointers
int i = 0;
for (i = 0; i < 3; i++) {
printf("%d\n", *(arr1[i]));//*(arr1[i]) Or you could write it as *(*(arr1+i))
}
}#include <stdio.h>
void main() {
int a[] = { 1,2,3,4,5 };
int b[] = { 2,3,4,5,6 };
int c[] = { 3,4,5,6,7 };
int* arr[] = {a,b,c};
int i = 0;
for (i = 0; i < 3; i++) {
int j = 0;
for (j = 0; j < 5; j++) {
printf("%d ", *(arr[i] + j));//*(arr[i] + j) Or you could write it as arr[i][j]
}
printf("\n");
}
}arr[i] Equivalent to :*(arr+i)
Array pointer
Shaping the pointer : Pointer to the shape
Character pointer : Pointer to character
Array pointer : Pointer to array

#include <stdio.h>
void main() {
int arr[] = { 1,2,3,4,5 };
//arr: Address of the first element of the array ——arr[0] The address of
//&arr: The address of the entire array
int(*parr)[10] = &arr;//parr Is an array pointer , It stores the address of the array
double* d[5];// Pointer array
double* (*p)[5] = &d;// Array pointer of pointer array
}& Array name and array name
#include <stdio.h>
void main() {
int arr[10] = { 0 };
int* p1 = arr;
int (*p2)[10] = &arr;
printf("%p\n", arr);
printf("%p\n", &arr);
printf("%p\n", arr+1);// There are more addresses than above 4 byte
printf("%p\n", &arr+1);// There are more addresses than above 40 byte
// From this point of view , The step size of the array address is several times of the element size of the address step size of the first element of the array
}#include <stdio.h>
void main() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int(*p)[10] = &arr;
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d ", *((*p) + i));//p Is the address of the entire array ,*p representative arr
}
}Be careful : The array name represents the address of the first element , The first element of a two-dimensional array is the first line , That is, the address of the first row of one-dimensional array, that is, the pointer of a one bit array
#include <stdio.h>
print(int(*p)[5], int r, int c) {
int i = 0, j = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("%d ", *(*(p + i) + j));
//p+i——i+1 The address of the row array ,*(p+i)——i+1 Array of rows ,*(p+i)+j——i+1 Row array No j+1 The address of the line element ,*(*(p + i) + j)——i+1 Row array No j+1 Row element
}
printf("\n");
}
}
void main() {
int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7} };
print(arr, 3, 5);
}About the meaning of array code
int arr[5];// Shape array 5 Elements , Each element is int
int* p1[10];// Pointer array can store 10 individual int Pointer to type
int(*p2)[10];//int type 10 Pointer to an array of elements
int(*p3[10])[5];// Store array pointers ( The pointer points to int type 5 Array of elements ) Array of ( This array can store 10 Array pointers )Array parameters and pointer parameters
One dimensional array parameters

1. There's a 10 An integer array of elements , Accept with an automatic length shaping array ——ok
2. There's a 10 An integer array of elements , use 10 An integer array of elements accepts ——ok
3. Pass an array name (int Type first element address ) use int Type pointer accepts ——ok
4. Pass an array of integer pointers that can hold 20 Elements , use 20 Element shaping pointer array accepts ——ok
5. Pass an array name (int* Type first element address ), Accept with an integer secondary pointer ——ok
Two dimensional array parameters

About the former 3 From the following notes 1,3 all ok
4. Two dimensional array parameters , Its array name represents the address of the first element, that is, the address of the first row of one-dimensional array , From this point of view , Only 6ok
Be careful : Only dual addresses can be accepted with secondary pointers , When accepting, you have to see whether the type is right .
First level pointer parameter transfer
#include <stdio.h>
void print(int* p,int sz) {
int i = 0;
for (i = 0; i < sz; i++) {
printf("%d ", *(p + i));
}
}
void main() {
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr1;
int sz = sizeof(arr1) / sizeof(arr1[0]);
print(p, sz);
}The secondary pointer transmits parameters
#include <stdio.h>
void test(int** p) {
**p = 20;
}
void main() {
int a = 10;
int* pa = &a;
int** ppa = &pa;
test(ppa);
int b = 7;
int* arr[10] = { &b };
test(arr);
printf("%d\n", a);
printf("%d\n", *(arr[0]));
}A function pointer
Definition : Pointer to function ( A pointer to the address of the function )
#include <stdio.h>
int Add(int a,int b) {
return a + b;
}
void main() {
//& Function name —— Get the address of the function
printf("%p\n", &Add);// From this point of view , You can get the address of the function
// Be careful : Array name !=& Array name , But the function name ==& Function name ( Function has no address of the first element )
printf("%p\n", Add);
// Save function pointer
int (*p1)(int, int) = &Add;// Of course , Because the function name ==& Function name , So the & Omission
printf("%p\n", p1);
int (*p2)(int, int) = Add;
printf("%p\n", p2);
// Function call
int ret=(*p1)(3, 5);
int res=p2(3, 5);// Call directly with the function name ——p2==&Add and &Add==Add therefore p2==Add
printf("%d\n",ret);
printf("%d\n",res);
}Function pointer type : return type (*)( Parameter type list )
Code reading

Declaration that the function pointer type is the return value
void (* signal(int,void(*)(int)))(int);
replace ——
void(*)(int) signal(int,void(*)(int));
// Easy to write
typedef void(*pfun_t)(int);// Yes void(*)(int) Rename the function pointer type to pfun_t;
pfun_t signal(int, pfun_t);Function pointer array
Definition : An array of function pointers
#include <stdio.h>
int Add(int a,int b) {
return a + b;
}
int Sub(int a, int b) {
return a - b;
}
void main() {
int (*p1)(int, int) = Add;
int (*p2)(int, int) = Sub;
int (*parr[2])(int, int) = {p1,p2};// Function pointer array can store 2 The return values are int parameter list 2 individual int Function pointer of
int ret = (*parr[1])(8, 5);// Function call through function pointer array 1
int res = parr[0](8, 5);// Function call through function pointer array 2
printf("%d\n", ret);
printf("%d\n", res);
}A pointer to an array of function pointers
Definition : Get the address of the function pointer array
int arr1[5];// Shape array
int (*p1)[5]=&arr1;// A pointer to an integer array
int* arr2[5];// An array of integer pointers
int* (*p2)[5] = &arr2;// Integer pointer array pointer
int (*p3)(int,int);// A function pointer
int (*p4[5])(int, int);// An array of function pointers
int (*(*p4)[5])(int, int);// Function pointer array pointer Callback function
Definition :

give an example :b The function has a Function pointer as parameter , When passed a The pointer of the function calls a function , This mechanism is called callback function mechanism , The function called by the pointer in turn is called a callback function .
#include <stdio.h>
int Add(int a,int b) {
return a + b;
}
int AP(int(*p)(int,int)) {
return p(2, 3);// Here I have directly omitted *
}
void main() {
int(*ap)(int(*)(int, int)) = &AP;
int ret=ap(&Add);
printf("%d\n", ret);
return;
}#include <stdio.h>
int Add(int a,int b) {
return a + b;
}
int AP(int(*p)(int,int)) {
return p(2, 3);// Here I have directly omitted *
}
void main() {
// How to write it 2—— Anyway &AP==ap==AP==*ap So will *ap use AP replace
int ret=AP(&Add);
printf("%d\n", ret);
return;
}边栏推荐
- Jerry's maximum volume prompt sound cannot be broadcast [article]
- HowNet and Wanfang database download papers for free ----- several times faster than connecting to the school intranet (some schools Wanfang database does not support downloading)
- Data collection: skillfully using Bloom filter to extract data summary
- 领导:谁再用redis过期监听实现关闭订单,立马滚蛋!
- OpenCV(三)——图像分割
- OpenCV(一)——图像基础知识
- Gurobi——GRBEnv
- Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
- Insert pictures in word to maintain high DPI method
- Servlet中文乱码setContentType设置无效
猜你喜欢

清晰的认识Torchvision(思维导图版)

牛客题目——链表的奇偶重排、输出二叉树的右视图、括号生成、字符流中第一个不重复的字符

The class declared by the scala object keyword directly calls methods and associated objects
Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)

Rotate string left

Implementation of filler creator material editing tool

C语言之指针进阶
![Jerry's built-in touch parameters for modification [chapter]](/img/6b/38c3ad28a7256e5e41bb444d0993db.png)
Jerry's built-in touch parameters for modification [chapter]

牛客题目——判断是不是完全二叉树、平衡二叉树

CCF-201312-1
随机推荐
JDBC程序实现完整步骤
C语言之分支循环语句
【论文阅读】Single- and Cross-Modality Near Duplicate Image PairsDetection via Spatial Transformer Compar
Servlet用Cookie实现用户上次登录时间
Servlet uses cookies to realize the last login time of users
Polynomial locus of order 5
Jupyter creates a virtual environment and installs pytorch (GPU)
数据采集之:巧用布隆过滤器提取数据摘要
Segment tree beats~
Snowflake ID (go Implementation)
C语言之指针初级
Get the array list of the previous n days and the previous and subsequent days of the current time, and cycle through each day
C语言之数组
201403-1
OpenCV(一)——图像基础知识
(2) Dynamic convolution of dynamic convolution
雪花ID(Go 实现)
(二)动态卷积之Dynamic Convolution
ROS - error in running.Py file in the project
Stylelint check error: unexpected missing generic font family font family no missing generic family keyword