当前位置:网站首页>Potential learning C language - pointer explanation (Advanced)
Potential learning C language - pointer explanation (Advanced)
2022-06-29 03:16:00 【Bitniuniu】
Advance of pointer
First, let's review the basic concepts :
1. The pointer is a variable , It's used to store the address , The address uniquely identifies a piece of memory space .
2. The size of the pointer is fixed 4/8 Bytes (32 Bit platform /64 platform ).
3. Pointers have different types , The type of pointer determines the type of pointer +- Integer step size , The permission of pointer dereference operation .
4. The operation of the pointer
Character pointer
General usage :
int main()
{
char ch = 'w';
char* pc = &ch;
return 0;
}int main()
{
char arr[] = "abcdef";
char* pc = arr;
printf("%s\n", arr);
printf("%s\n", pc);
return 0;
}int main()
{
const char* p = "abcdef";//"abcdef" Is a constant string
printf("%c\n", *p);
printf("%s\n", p);
return 0;
}What is the output of the next code :
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcdef";
const char* p1 = "abcdef";
const char* p2 = "abcdef";
if (arr1 == arr2)
{
printf("hehe\n");
}
else
{
printf("haha\n");
}
return 0;
}answer :haha
arr1 And arr2 Different storage addresses
What is the output value of this code ?
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcdef";
const char* p1 = "abcdef";
const char* p2 = "abcdef";
if (p1 == p2)
{
printf("hehe\n");
}
else
{
printf("haha\n");
}
return 0;
}answer :hehe
Constant strings cannot be modified ,p1 p2 All point to the same space ( The string is the same , Save the same address .)
When several pointers point to the same string , They actually point to the same block of memory , However, when initializing different arrays with the same constant string, different memory blocks will be opened up .
Pointer array
Pointer arrays are arrays , Used to store pointers
int main()
{
int arr[10] = { 0 };// integer array
char ch[5] = { 0 };// A character array
int* parr[4];// An array of integer pointers - Pointer array
char* pch[5];// An array of character pointers - Pointer array
return 0;
}application :
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 2,3,4,5,6 };
int arr3[] = { 3,4,5,6,7 };
int* parr[] = { arr1,arr2,arr3 };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d", *(parr[i] + j));
}
printf("\n");
}
return 0;
}Array pointer
int main()
{
int* p = NULL;//p It's an integer pointer - Pointer to an integer - Can store integer addresses
char* pc = NULL;//pc It's a character pointer - Pointer to character - An address where characters can be stored
// Array pointer - Pointer to array - The address where the array is stored
int arr[10] = { 0 };
//arr - First element address
//&arr[0] - First element address
//&arr - Address of array
return 0;
}int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int (*p)[10] = &arr;//[] Is greater than *
// above p It's an array pointer practice : Make up pa The type of
int main()
{
char* arr[5];
pa =&arr
return 0;
} char*(*pa)[5] = &arr;pa The array pointed to is 5 Elements
* explain pa Is a pointer
pa The element type of the array pointed to is char*
Usage of array pointers
// Parameters are in the form of arrays
void print1(int arr[3][5], int x, int y)
{
int i = 0;
int j = 0;
for (i = 0; j < y; j++)
{
for (j = 0; j < y; j++)
{
printf("%d", arr[i][j]);
}
printf("\n");
}
}
// Parameters are in the form of pointers
void printf(int(*p)[5], int x, int y)
{
int i = 0;
for (i = 0; i < x; i++)
{
int j = 0;
for (j = 0; j < y; j++)
{
printf("%d", *(*(p + i) + j));
}
printf("\n");
}
}
int main()
{
int arr[3][5] = { {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7} };
print1(arr, 3, 5);
print2(arr, 3, 5);
return 0;
}int arr[5]; arr It's a 5 An integer array of elements
int* parr1[10]; parr1 Is an array , Array has 10 Elements , The type of each element is int*,parr1 Is the number of pointers Group
int(*parr2)[10]; parr2 It's a pointer , This pointer points to an array , Array has 10 Elements , Of each element The type is int - parr2 It's an array pointer
int(*parr3[10])[5]; parr3 Is an array ,parr3 It's an array name , The array has 10 Elements , Each element is a number Group pointer , The array pointed to by the array pointer is 5 Elements , Each element is int
summary :
int main()
{
// Character pointer
char ch = 'w';
char* p = &ch;
const char* p2 = "abcdef";// Pointer array - Array - An array of pointers
int* arr[10];
char* ch[5];
// Array pointer - Pointing to the array
//int *p3;// Integer pointer - Pointer to the shape
//char* p4;// Character pointer - Pointing to a character
int arr2[5];// Array
int(*pa)[5] = &arr2;// Take the address of the array ,pa Is an array pointer
return 0;
}Array parameters , Pointer parameter
Array parameters
void test(int arr[3][5])
{}
void test1(int[][5])
{}
void test2(int arr[3][])//err
int main()
{
int arr[3][5] = { 0 };
test(arr);// Two dimensional array parameters
test1(arr);
test2(arr);
return 0;
}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 , This makes it easier to calculate .
The address of the first element of a two-dimensional array is the address of the first row of the array .
First level pointer parameter transfer
void print(int* p, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%dn", *(p + i));
}
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int* p = arr;
int* p = arr;
int sz = sizeof(arr) / sizeof(arr[0]);
// First level pointer p, Pass to function
print(p, sz);
return 0;
}But when the parameter part of a function is a first-order pointer , What parameters can a function accept ?
void test1(int* p)
{}
int main()
{
int a = 10;
test1(&a);
test1(p1);
return 0;
}The secondary pointer transmits parameters
void test1(int** ptr)
{
printf("num = %d\n", **ptr);
}
int main()
{
int n = 10;
int* p = &n;
int** pp = &p;
test(pp);
test(&p);
return 0;
}void test(int**p)
{}
int main()
{
int *ptr;
int** pp = &ptr;
test(&ptr);
test(pp);
int* arr[10];
test(arr);// Pointer arrays can also
return 0;
}It can be transmitted : The address of the first level pointer , The secondary pointer variable itself , The array name of the array that holds the first level pointer
A function pointer
Array pointer - Pointer to array
A function pointer - Pointer to function - A pointer to the function address
int Add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int a = 10;
int b = 20;
int arr[10] = { 0 };
printf("%p\n", &Add);
printf("%p\n", Add);
//& Function name and Function name All are the addresses of functions
return 0;
}int Add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int a = 10;
int b = 20;
//int arr[10] = { 0 };
//int(*p)[10] = &arr;
int(*pa)(int, int) = Add;
printf("%d\n", (*pa)(2, 3));//5
return 0;
}
Look at the following two codes :
Code 1:
(*(void)(*)()0)();( void(*)() )0 ----> (*( void(*)() )0) ();
hold 0 Force type to :void(*)() Function pointer type - 0 Is the address of a function , call 0 Function at address
Code 2:
void(*signal(int, void(*)(int)))(int);void(*)(int) ----> Function pointer type
void(* )(int) ----->
analogy :int Add(int,int)
example :
//signal It's a function declaration
//signal The arguments to the function are 2 individual , The first is int type , The second is function pointers , The function pointer points to a function whose argument is int, The return type is void
//signal The return type of a function is also a function pointer , The function pointer points to a function whose argument is int, The return type is void
void (* signal(int, void(*)(int)) )(int);
// simplify
typedef void(*pfun_t)(int);
pfun_t signal(int, pfun_t);
typedef unsigned int uint;Function pointer array
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
// Pointer array
int* arr[5];
// You need an array , This array can hold the addresses of four functions - An array of function pointers
int(*Pa)(int, int) = Add;//Sub/Mul/Div
int(*parr[4])(int, int)={ Add,Sub,Mul,Div};// An array of function pointers
int i = 0;
for (i = 0; i < 4; i++)
{
printf("%d\n", parr[i](2, 3));//5 -1 6 0
}
return 0;
}example :
my_strcpy(char* dest, const char* src);
// Write a function pointer pf, Can point to my_strcpy
// Write an array of function pointers pfarr, Be able to store 4 individual my_strcpy Address of function
char* (*pf)(char*, const char*);
char* (*pfarr[4])(char*, const char*);Purpose of function pointer array : Transfer table
example : Calculator :
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 x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
do
{
menu();
printf(" Please select :>");
scanf("%d",&input);
switch (input)
{
case 1:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n",Add(x, y));
break;
case 2:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Sub(x, y));
break;
case 3:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Mul(x, y));
break;
case 4:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Div(x, y));
break;
case 0:
printf(" sign out \n");
default:
printf(" Wrong choice \n");
break;
}
}while(input);
return 0;
}reform : Use function pointer array
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 x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
int (*pfArr[5])(int, int) = { 0,Add,Sub,Mul,Div };
do
{
menu();
printf(" Please select :>");
scanf("%d",&input);
if (input >= 1 && input <= 4)
{
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
int ret = pfArr[input](x, y);
printf("%d\n", ret);
}
else if (input == 0)
{
printf(" sign out \n");
}
else
{
printf(" Wrong choice \n");
}
}while(input);
return 0;
}pfArr Is an array of function pointers
A pointer to an array of function pointers
int main()
{
int arr[10] = { 0 };
int(*P)[10] = &arr;// Take the address of the array
int(*pf)(int,int);// A function pointer
int(*pfArr[4])(int, int);//pfArr Is an array - An array of function pointers
//ppfArr It's a point [ Function pointer array ] The pointer to
int(*(*ppfArr)[4])(int, int) = &pfArr;
//ppfArr It's an array pointer , The array that the pointer points to is 4 Elements
// The type of each element of the array pointed to is a function pointer int(*)(int,int)
return 0;
}summary :
int Add(int x, int y)
{
return x + y;
}
int main()
{
// Pointer array
//int* arr[10];
// Array pointer
//int*(*pa)[10] = &arr;
// A function pointer
int (*pAdd)(int, int) = Add;//&Add
//int sum =(*pAdd)(1,2);
//int sum =*pAdd(1,2);
//printf("sum = %d\n",sum);
// An array of function pointers
int(*pArr[5])(int, int);
// A pointer to an array of function pointers
int(* (*pArr)[5])(int, int) = &pArr;
return 0;
}
Callback function
A callback function is a function called through a function pointer . If you 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 , We call it a callback function . The callback function is not called directly by the function's implementer , It is called by the other party when a specific event or condition occurs , Used to respond to the event or condition .
give an example :
void print(char *str)
{
printf("hehe:%s", str);
}
void test(void(*p)(char*))
{
printf("test\n");
p("bit");
}
int main()
{
test(print);
return 0;
}application
do
{
menu();
printf(" Please select :>");
scanf("%d",&input);
switch (input)
{
case 1:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n",Add(x, y));
break;
case 2:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Sub(x, y));
break;
case 3:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Mul(x, y));
break;
case 4:
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", Div(x, y));
break;
case 0:
printf(" sign out \n");
default:
printf(" Wrong choice \n");
break;
}In this code
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);There have been many times , How to use callback function to simplify code ?
void Calc(int(*pf)(int,int))
{
int x = 0;
int y = 0;
printf(" Please enter two operands :>");
scanf("%d%d", &x, &y);
printf("%d\n", pf(x, y));
}
switch (input)
{
case 1:
Calc(Add);
break;
case 2:
Calc(Sub);
break;
case 3:
Calc(Mul);
break;
case 4:
Calc(Div);
break;
case 0:
printf(" sign out \n");
default:
printf(" Wrong choice \n");
break;Add :
int main()
{
int a = 10;
int* pa = &a;
void* p = &a;//void* Pointer to type , Can accept any type of address
// void* Pointer of type cannot be dereferenced
//void* A pointer to a type cannot be +— The operation of integers
return 0;
}qsort - Library function - Sort
Quick sort
The first parameter : The address of the first element of the array to be sorted
The second parameter : Number of elements of the array to be sorted
The third parameter : The size of each element of the array to be sorted - Unit is byte
Fourth parameter : It's a function pointer , Compare the addresses of the functions used by the two functions - This function is implemented by the user himself
The two arguments to the function pointer are : The addresses of the two elements to be compared
边栏推荐
- 1110: nearest common ancestor (function topic)
- MATALB signal processing - signal transformation (7)
- PWN攻防世界Level2
- 測試入門——集成測試
- 微信小程序安全登录,必须先校验微信返回openid,确保信息唯一性。
- Square root of X
- Tkinter Huarong Road 4x4 Tutorial 4
- Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color
- PWN攻防世界guess_num
- Digital twin application of smart Park Based on Web GIS aerial photography
猜你喜欢
![Movement state change of monitoring device of Jerry's watch [chapter]](/img/ff/cbc9e50a7d64e943f9f9924eadb184.jpg)
Movement state change of monitoring device of Jerry's watch [chapter]

图扑软件智慧能源一体化管控平台

如何理解MySQL的索引?
![Jerry's watch stops moving [chapter]](/img/04/0238701722e1f90410b37385b164e2.jpg)
Jerry's watch stops moving [chapter]

PWN attack and defense world level2

2022-2028 global long wave infrared camera and camera core industry research and trend analysis report

解决allegro中测量距离时,点击一个点后光标闪烁的问题

2022-2028 global bubble CPAP system industry survey and trend analysis report

Concise words tell about technical people who must master basic IT knowledge and skills. Part 1

MATALB signal processing - signal transformation (7)
随机推荐
Getting started with testing - integration testing
Yyds dry inventory everything a primary developer should know about activity
Is it safe to open a stock account by mobile phone? Is it difficult to open an account?
Jerry's watch begins to move [chapter]
Allegro's method of canceling the routing of a good line
Square root of X
测试入门——集成测试
如何在关闭socket连接的时候跳过TIME_WAIT的等待状态
There's a mystery behind the little login
FPGA (VIII) RTL code IV (basic circuit design 1)
蓝桥杯2022初赛——扫雷
PWN攻防世界guess_num
Several ways to add breakpoints using GDB
allegro对走好的线取消走线的方法
Problème - Ajouter shellerror: permissions d'instrumentation pour le périphérique: vérifier les règles udev.
[North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss
Altium Designer中从已有的PCB中导出所有元件的封装的方法
2022-2028 global secondary butyl lithium industry research and trend analysis report
Démarrer le test - test d'intégration
快速排序,查询序列的第K大的数