当前位置:网站首页>[dish of learning notes dog learning C] advanced pointer
[dish of learning notes dog learning C] advanced pointer
2022-07-24 10:35:00 【Jiang Junzhu】
List of articles
- One 、const Modify pointer variables
- Two 、 Character pointer
- 3、 ... and 、 Pointer array
- Four 、 Array pointer
- 5、 ... and 、 Array parameters 、 The pointer passes the parameter
- 6、 ... and 、 A function pointer
- 7、 ... and 、 Function pointer array
- 8、 ... and 、 A pointer to an array of function pointers
- Nine 、 Callback function
One 、const Modify pointer variables
- const If you put it in * Left side , It modifies what the pointer points to , Ensure that the content pointed to by the pointer cannot be changed by the pointer . But the contents of the pointer variable itself are variable ;
- const If you put it in * To the right of , The modification is the pointer variable itself , It ensures that the contents of pointer variables cannot be modified , But what the pointer points to , It can be changed by the pointer .
void test1()
{
int n = 10;
int m = 20;
int* p = &n;
*p = 20;
p = &m;
}
void test2()
{
// Code 2
int n = 10;
int m = 20;
const int* p = &n;
*p = 20;// Report errors
p = &m;
}
void test3()
{
int n = 10;
int m = 20;
int* const p = &n;
*p = 20;
p = &m; // Report errors
}
int main()
{
// Test none cosnt Of
test1();
// test const Put it in * Left side
test2();
// test const Put it in * To the right of
test3();
return 0;
}
Two 、 Character pointer
int main() {
// The essence is to put "hello" The address of the first character of this string is stored in ps in
char* ps = "hello";
printf("%c\n", *ps);
return 0;
}
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
const char* str3 = "hello bit.";
const char* str4 = "hello bit.";
if (str1 == str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if (str3 == str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
3、 ... and 、 Pointer array
Pointer arrays are essentially arrays , Stored in the array are pointers .
int main() {
// An array of integer pointers
//int* arr[3];
int a[5] = {
1,2,3,4,5 };
int b[] = {
2,3,4,5,6 };
int c[] = {
3,4,5,6,7 };
// Simulated a two-dimensional array , But it's not a two-dimensional array , Two dimensional arrays are stored consecutively .
int* arr[3] = {
a, b, c };
int i = 0;
for (int i = 0; i < 3; i++) {
int j = 0;
for(j = 0;j < 5; j++){
//printf("%d ", *(arr[i] + j));
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Four 、 Array pointer
Pointer to array , Pay attention to the difference between array pointer and array pointer .
int (*p)[10];
explain :p The first and * combination , explain p Is a pointer variable , Then point to a size of 10 An array of integers . therefore p It's a pointer , Point to an array , It's called array pointer .
Be careful :[] Priority is higher than * Of , So we have to add () To guarantee p The first and * combination .
int main() {
int arr[10] = {
1,2,3,4,5 };
int (*parr)[10] = &arr;// What we get is the address of the array
//parr Is an array pointer - Where is the address of the array
double* d[5];
double* (*pd)[5] = &d;//pd Is an array pointer
return 0;
}
We basically don't use array pointers to store the address of a one-dimensional array
5、 ... and 、 Array parameters 、 The pointer passes the parameter
- One dimensional array parameters
// Array parameters , Arrays accept
void test(int arr[])
{
}
// Array parameters , Arrays accept , there 10 It doesn't make sense
void test(int arr[10])
{
}
// The array parameter actually passes the address of the first element , There is no problem receiving with pointer
void test(int* arr)
{
}
//arr2 Itself is an array of pointers , You can use a pointer array to receive
void test2(int* arr[20])
{
}
// The array name represents the address of the first element , That's one int* The address of
//int* Is a first level pointer ,
// here arr2 It is equivalent to taking a primary pointer address
// that arr2 It is equivalent to a secondary pointer , You can receive... With a secondary pointer
void test2(int** arr)
{
}
int main() {
int arr[10] = {
0 };
int* arr2[20] = {
0 };// Array of integer pointers
test(arr);
test2(arr2);
return 0;
}
- Two dimensional array parameters
void test(int arr[3][5])
{
}
// Two dimensional array parameters , Function parameter design can only omit the first [] The number of
// Because for a two-dimensional array , I don't know how many lines there are , But you have to know how many elements in a row
void test(int arr[][])// Report errors
{
}
void test(int arr[][5])
{
}
// The array name of a two-dimensional array also represents the address
// But it represents the address of the first element , Instead of the address of the first element
// Cannot receive with a first level pointer
void test(int* arr)
{
}
//int* arr[5] It's an array of Pointers , Cannot receive two-dimensional arrays
void test(int* arr[5])
{
}
// The array pointer can point to an array containing 5 individual int One dimensional array of type elements
// The array name of a two-dimensional array represents the address of the first row of elements
// It happens to be a 5 individual int One dimensional array of type elements
void test(int (*arr)[5])
{
}
// The address of a one-dimensional array is passed , You can't use a secondary pointer to receive
void test(int** arr)
{
}
int main()
{
int arr[3][5] = {
0 };
test(arr);
}
- First level pointer parameter transfer
void print(int* p, int sz) {
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d\n", *(p + i));
}
}
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9 };
int* p = arr;
int sz = sizeof(arr) / sizeof(arr[0]);
// First level pointer p, Pass to function
print(p, sz);
return 0;
}
- The secondary pointer transmits parameters
void test(int** ptr) {
printf("num = %d\n", **ptr);
}
int main()
{
int n = 10;
int* p = &n;
int** pp = &p;
test(pp);
// The address of the first level pointer
// The secondary pointer itself is used to store and pointer addresses
test(&p);
int* arr[10] = {
0 };
// You can pass an array of pointers
// Pointer array stores pointers, that is, addresses
// It's kind of like passing a first-class pointer address
test(arr);
return 0;
}
6、 ... and 、 A function pointer
A function pointer is a pointer that stores the address of a function .
int Add(int x, int y){
return x + y;
}
int main() {
//pf It's a function pointer
int (*pf)(int, int) = &Add;
//&Add == Add
//&Add、Add All represent the address of the function
printf("%p\n", &Add);
printf("%p\n", Add);
printf("%p\n", pf);
// Actually, here * There is no real intention
// Its function is to let us know that this is a pointer
int ret1 = (*pf)(3, 5);
printf("%d\n", ret1);
//int (*pf)(int, int) = Add;
//pf === Add
// Function calls can be written as Add(3, 5)
// So it can be written as pf(3, 5)
int ret2 = pf(3, 5);
printf("%d\n", ret2);
return 0;
}
7、 ... and 、 Function pointer array
Function pointer array is used to store function pointers of the same type .
int Add(int x, int y) {
return x + y;
}
int Sub(int x, int y) {
return x - y;
}
int main() {
int (*pf1)(int, int) = Add;
int (*pf2)(int, int) = Sub;
//pfArr Function pointer array , Store function pointers of the same type
int (*pfArr[2])(int, int) = {
Add, Sub };
return 0;
}
8、 ... and 、 A pointer to an array of function pointers
The pointer to the array of function pointers is a pointer , The pointer points to an array , The elements of the array are function pointers .
void test(const char* str) {
printf("%s\n", str);
}
int main()
{
// A function pointer pfun
void (*pfun)(const char*) = test;
// An array of function pointers pfunArr
void (*pfunArr[5])(const char* str);
pfunArr[0] = test;
// Pointer to function array pfunArr The pointer to ppfunArr
void (*(*ppfunArr)[5])(const char*) = &pfunArr;
return 0;
}
Nine 、 Callback function
A callback function is a function called through a function pointer . Put the pointer of the function ( Address ) Pass as argument to another function , When this pointer is used to call the function it points to , Let's just say this is a callback function . The callback function is not called directly by the function's implementer , It's called by another party when a particular event or condition occurs , Used to respond to the event or condition .
【 Learning notes Dog learn C】 Getting to know the pointer
【 Learning notes Dog learn C】 Initial order of pointer
边栏推荐
- CMS vulnerability recurrence - ultra vires vulnerability
- MySQL——锁
- NLP introduction + practice: Chapter 2: introduction to pytorch
- Basic SQL operations
- Qt创建应用程序托盘及相关功能
- Intranet remote control tool under Windows
- zoj-Swordfish-2022-5-6
- Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!
- Golang migrate is easy to use
- Problem solving -- question 283 of leetcode question bank
猜你喜欢

MySQL - 普通索引

CMS vulnerability recurrence - ultra vires vulnerability

ffmpeg花屏解决(修改源码,丢弃不完整帧)

机器学习小试(11)验证码识别测试-使用Qt与Tensorflow2进行深度学习实验

MySQL - 全文索引

zoj-Swordfish-2022-5-6

Google cooperates with colleges and universities to develop a general model, proteogan, which can design and generate proteins with new functions

WEB安全基础 - - -文件上传(文件上传绕过)

N叉树、page_size、数据库严格模式修改、数据库中delect和drop的不同

MySQL - full text index
随机推荐
《nlp入门+实战:第二章:pytorch的入门使用 》
Differential restraint system -- 1 and 2 -- May 27, 2022
Kotlin domain specific language (DSL)
binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
js函数调用下载文件链接
Sentinel 三种流控模式
OSPF includes special area experiments, mGRE construction and re release
The method modified by private can be accessed through reflection. What is the meaning of private?
Mysql InnoDB下联合索引的索引数量?
给你的网站加一个爱发电角标
第五章 修改实现(IMPL)类
Interpretation of websocket protocol -rfc6455
pom文件dependency中的 scope用法
How many indexes are associated indexes under MySQL InnoDB?
The role of glpushmatrix and glpopmatrix
The paper of gaojingjian center was selected into the ACL 2022 of the international summit to further expand the privacy computing capacity of Chang'an chain
DSP CCS software simulation
Try the video for 5 minutes [easy to understand]
Build a live broadcast platform based on webrtc
MySQL - 多列索引