当前位置:网站首页>Kill the general and seize the "pointer" (Part 2)
Kill the general and seize the "pointer" (Part 2)
2022-06-27 22:37:00 【Desperate Azi】
The same beautiful appearance , One in a million interesting souls
Pointer is C The soul of language
There should be many friends who are still confused about the pointer
In this issue Violet Then I will take you through the pass and kill the generals , Catch “ The pointer ”

Catalog
3.1 Definition of array pointer
3.2 & Array name vs Array name
6. A pointer to an array of function pointers
1. Character pointer
Character pointer : Point to character Of The pointer , The pointer type is char*
char ch = 'a';
char* pc = &ch;General usage :
#include<stdio.h>
int main()
{
char ch = 'a';
char* pc = &ch;
*pc = 'b';
printf("%c\n", ch);
return 0;
}There's another way to use it :
#include<stdio.h>
int main()
{
const char* pc = "abcdef";
printf("%s\n", pc);
return 0;
}reflection 1: Why? const char* pc = "abcdef" Add in front. const?
answer : Because character pointers pc Pointing to Constant string , Constants cannot be changed , So add const Avoid modifying constant strings .
reflection 2:const char* pc = "abcdef", It's a abcdef String into character pointer variable pc Medium ?
answer : No , Is to put the string First character Of Address Put in pc in , That is the a My address is in pc in .

Interview questions :
#include<stdio.h>
int main()
{
char arr[] = { "abcdef" };
char brr[] = { "abcdef" };
const char* crr = "abcdef";
const char* drr = "abcdef";
if (arr == brr)
{
printf("arr and brr are same\n");
}
else
{
printf("arr and brr not are same\n");
}
if (crr == drr)
{
printf("crr and drr are same\n");
}
else
{
printf("crr and drr not are same\n");
}
return 0;
}Running results :

arr and brr Is a character array , It's a abcdef Stored in a character array , Their memory is not the same , So their addresses are different . and crr and drr It's a character pointer , It's a abcdef The address of the first element of the constant string is stored in the character pointer , So they point to the same memory space . 
2. Pointer array
Pointer array : It's a Array , Array Each element is a type pointer
int* arr[10]; // An array of integer pointers First arr Follow me first [10] It is an array with ten elements , Every element is int* Type of The pointer .
3. Array pointer
3.1 Definition of array pointer
Array pointer : It's a The pointer , Point to a Array
int arr[6] = { 1, 2, 3, 4, 5, 6 };
int(*pi)[6] = &arr;pi The first and * The combination shows that it is a Pointer to the variable ,[6] That means it points to a Array , Array has 6 Elements ,int Explain that each element is int type .
- notes :[] Priority is higher than * The no. , So we have to add () To guarantee p The first and * combination .
3.2 & Array name vs Array name
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };- Array name arr : Express First element Of Address
- & Array name : Express Entire array Of Address

arr and &arr They print out the same address , But they mean different things .arr Represents the address of the first element of the array , and &arr Represents the address of the entire array ,arr + 1 Skipped an element , and &arr + 1 Skipped the entire array .

sizeof(arr[0]) It's calculation arr[0] The amount of space taken up , and sizeof(arr) It's calculation Entire array The amount of space taken up .
Conclusion :& Array name and sizeof( Array name ) Express Entire array , The rest Array name All means Address of the first element of the array .
3.3 The use of array pointers
since Array pointer Pointing to Array , that Array pointer Which is stored in Address of array .
#include<stdio.h>
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(*pi)[10] = &arr;
return 0;
}hold Array arr The address of Assign values to array pointer variables p , But we seldom write code like this , Because there is no point in writing code like this .
Generally, we use array pointers for Two dimensional array
#include<stdio.h>
void print_arr(int(*arr)[3], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
print_arr(arr, 3, 3);
return 0;
}Array name arr, Represents the address of the first element . But the first element of a two-dimensional array is a two-dimensional array first line , So the message here is arr, In fact, it's equivalent to The address on the first line , yes One dimensional array address .
After learning pointer array and array pointer, let's review and see what the following code means :
int arr[5];// integer array
int *parr1[10];// Array pointer
int (*parr2)[10];// Pointer array
int (*parr3[10])[5];//???int (*parr3[10])[5]: First parr3 Follow [10] The combination shows that it is an array , Each element is a pointer array type .
4. A function pointer
A function pointer : First, it is a pointer , It points to a function

Function name and & Function name : Are all expressions of the function Address
If you want to save the address of the function , You have to use function pointers

p Follow me first * combination That means it's The pointer , The one at the back () Indicates that it points to a Nonparametric functions , Ahead void Represents the return value void.
《 C Pitfalls and pitfalls 》 There are two codes in this book :
// Code 1
(*(void (*)())0)();take 0 Cast type to void(*)() Function pointer type , that 0 It is regarded as the address of a function , Then dereference the call , This is actually a function call , It's called 0 As a function at the address .
// Code 2
void (*signal(int , void(*)(int)))(int);signal Follow first Brackets combination , explain signal yes Function name , The first argument to the function argument is int, The second parameter is A function pointer ( The function parameter pointed to by the function pointer is int, The return type is void), Return value Also a A function pointer , The function parameter pointed to by the function pointer is int, The return type is void, So this is a Function declaration .
5. Function pointer array
Function pointer array : First of all, it's a Array , Every element of the array is The pointer Pointing to function .
int(*arr[10])();First arr Follow me first [10] The combination shows that it is a Array , Every element in the array is int(*)() Type of A function pointer .
Purpose of function pointer array : Transfer table
#include<stdio.h>
void menu()
{
printf("*********************\n");
printf("*** 1.add 2.sub ***\n");
printf("*** 3.mul 4.div ***\n");
printf("****** 0.exit *******\n");
printf("*********************\n");
}
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int a, b;
int input = 1;
int ret = 0;
int(*p[5])(int a, int b) = { 0, add, sub, mul, div }; // Transfer table
while (input)
{
menu();
printf(" Please select :");
scanf("%d", &input);
if ((input <= 4 && input >= 1))
{
printf(" Enter the operands :");
scanf("%d %d", &a, &b);
ret = (*p[input])(a, b);
}
else
printf(" Incorrect input \n");
printf("ret = %d\n", ret);
}
return 0;
}6. A pointer to an array of function pointers
A pointer to an array of function pointers : First of all, it's a The pointer , It points to a Array , Every element in the array is A function pointer .
void(*p)();// A function pointer
void(*p1[1])();// Function pointer array
void(*(*p2)[2])();// Pointer to function pointer array pointer 7. Callback function
A callback function is one that passes through The function called by the function pointer . If you take the function of The pointer ( Address ) Pass as a parameter to another function , When this pointer is used Call the function it points to when , Let's just say this is Callback function . The callback function is not controlled by this function The implementer of directly calls , But by... When a specific event or condition occurs Called by the other party , Used to enter the event or condition Row response .
qosrt Function USES :
#include <stdio.h>
//qosrt The user of the function has to implement a comparison function
int int_cmp(const void * p1, const void * p2)
{
return (*( int *)p1 - *(int *) p2);
}
int main()
{
int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
int i = 0;
qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof (int), int_cmp);// Pass the address of one function to another function
for (i = 0; i< sizeof(arr) / sizeof(arr[0]); i++)
{
printf( "%d ", arr[i]);
}
printf("\n");
return 0;
}
边栏推荐
- Use Fiddler to simulate weak network test (2g/3g)
- 管理系统-ITclub(上)
- Software defect management - a must for testers
- About the SQL injection of davwa, errors are reported: analysis and verification of the causes of legal mix of settlements for operation 'Union'
- gomock mockgen : unknown embedded interface
- 7 jours d'apprentissage de la programmation simultanée go 7 jours de programmation simultanée go Language Atomic Atomic Atomic actual Operation contains ABA Problems
- Typescript learning
- Gbase 8A OLAP analysis function cume_ Example of dist
- 使用sqlite3语句后出现省略号 ... 的解决方法
- YOLOv6:又快又准的目标检测框架开源啦
猜你喜欢

从学生到工程师的蜕变之路

美团20k软件测试工程师的经验分享

Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function

How to participate in openharmony code contribution

管理系统-ITclub(中)

信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可

Structured machine learning project (II) - machine learning strategy (2)

Go from introduction to practice -- shared memory concurrency mechanism (notes)

Use Fiddler to simulate weak network test (2g/3g)

Luogu p5706 redistributing fertilizer and house water
随机推荐
Use Fiddler to simulate weak network test (2g/3g)
BAT测试专家对web测试和APP测试的总结
如何做好功能测试?你确定不想知道吗?
解决本地连接不上虚拟机的问题
管理系統-ITclub(下)
使用sqlite3语句后出现省略号 ... 的解决方法
Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
How to prioritize the contents in the queue every second
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
九九乘法表——C语言
改善深层神经网络:超参数调试、正则化以及优化(三)- 超参数调试、Batch正则化和程序框架
Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
C # QR code generation and recognition, removing white edges and any color
Golang uses regularity to match substring functions
Management system itclub (Part 1)
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
Hash table - sum of arrays