当前位置:网站首页>Advanced C language - pointer 2 - knowledge points sorting
Advanced C language - pointer 2 - knowledge points sorting
2022-07-03 23:45:00 【Confused boy】
Catalog
One 、 One dimensional array parameters :
Two 、 Two dimensional array parameters :
3、 ... and 、 First level pointer parameter transfer :
Four 、 The secondary pointer transmits parameters :
5、 ... and 、 A function pointer :
6、 ... and 、 Two interesting pieces of code :
7、 ... and 、 Function pointer array :
8、 ... and 、 Application of function pointer array : Calculator
Nine 、 identify 3 A pointer to the :
1. Pointer to an integer array
2. Pointer to the array of integer pointers
3. A pointer to an array of function pointers
One 、 One dimensional array parameters :
// Code 1:
// One dimensional array parameters , It can be written as an array , It can also be written as a pointer
//1.
void test(int arr[])
{}
//2.
void test(int arr[10])
{}
//3.
void test(int *arr)
{}
//1.
void test2(int* arr[20])
{}
//2.
void test2(int** arr)
{}
int main()
{
int arr[10] = {0};
int* arr2[20] = {0};
test(arr);
test2(arr2);
}
Two 、 Two dimensional array parameters :
// Code 2:
// Two dimensional array parameters
void test(int arr[3][5])
{}//ok
void test(int arr[][])// You can omit lines , But you cannot omit columns
{}//err
void test(int arr[][5])
{} //ok
void test(int (*p)[5])// The element in the first line , Is the total 5 Elements , Every element is int type , So use the pointer of one-dimensional array to receive
{}//ok, Array uses array pointer to receive
int main()
{
int arr[3][5] = {0};
test(arr);//arr It represents the address of the first element , The first element represents the address of the first line , It's a one-dimensional array -- In the first row 5 Column , Arrays receive with array pointers
// What passed is the address of one-dimensional array , Then use the pointer of one-dimensional array to receive
return 0;
}
3、 ... and 、 First level pointer parameter transfer :
// Code 3:
// First level pointer parameter transfer
void print(int* p ,int sz)// The pointer p Is the first level pointer , So use the first level pointer to receive
{
// Array elements
int i = 0;
for (i = 0;i < sz;i++)
{
printf("%d ",p[i]);
}
}
int main()
{
int arr[] = {1,2,3,4,5,6};
int*p = arr;// First element address
int sz = sizeof(arr) / sizeof(arr[0]);
print(p,sz);
return;
}
Four 、 The secondary pointer transmits parameters :
// Code 4:
// The secondary pointer transmits parameters
void test(int **ppa)
{}
int main()
{
int a = 10;
int* pa = &a;
int** ppa = &pa;
int* arr[5];
test(ppa);// The way 1
test(&pa);// The way 2
test(arr);// The way 3:arr Represents the address of the first element , The first element is int* type ,int* Type addresses are received with secondary pointers
// The array name of pointer array is received with secondary pointer
return 0;
}
5、 ... and 、 A function pointer :
// Code 5:
// Shaping the pointer -- The address where the plastic surgery is stored
// Character pointer -- The address where the characters are stored
// Array pointer -- The address where the array is stored
// Function pointer variable -- Address where the function is stored
//& Array name -- Address of array
// Array name -- Address of the first element of the array
int Add(int x,int y)
{
return x + y;
}
int main()
{
printf("%p\n",&Add);//007F13E3
printf("%p\n",Add);//007F13E3
// Function name
//& Function name The addresses indicated by the two are the same
// Array pointer
int arr[10] = {0};
int(*parr)[10] = &arr;//*parr Express parr It's a pointer ,[10] Express arr Point to 10 Array of elements ,int Indicates that each element in the array is int type
// A function pointer
int (*pf)(int,int)= &Add;//*pf Express pf It's a pointer ,(int,int) Indicates the type of parameter ,int Represents the return type
// Summary type : Removing the function name is the type
int a = 10;//int
int arr[10];//int [10]
int(*parr)[10] = &arr;//int (*)[10] ,parr Array pointer variable
int (*pf)(int, int) = &Add;//int(*)(int,int)
//pf Is the address used to store the function - pf Is a function pointer variable
// The application of function pointer : Sometimes what you get is not a function , It's the address of the function
//1. Conventional application
int ret = Add(2,3);
printf("%d",ret);//5
//2. A function pointer
// Writing a : It is written so that beginners can better understand -- Take the address and dereference it
int(*pf)(int, int) = &Add;
ret = (*pf)(3, 4);// By dereferencing first pf, find Add() This function , And then in the
printf("%d", ret);//7
return 0;
// Write two : because &Add and Add Both represent function addresses , So it can also be written directly as Add
int(*pf)(int, int) = Add;// here Add and pf Same type , therefore pf and Add It's the same thing
ret = pf(3, 4);// So you can use it directly pf, Call directly
printf("%d", ret);//7
}
6、 ... and 、 Two interesting pieces of code :
// Code 6:
typedef void(* pfun_t)(int);// hold void(*)(int) Redefined as pfun_t
//typedef int int32 // But the formal grammar written above does not allow
int main()
{
// Code 1:
(*((void(*)()) 0))();
// This code is a function call
// analysis :
//1. In the code 0 Cast to type void(*)() The address of a function of
//2. Quoting 0 Address , To call 0 Address this function , The called function is parameterless , The return type is void
// Code 2
void (*signal(int, void(*)(int)))(int);
// This code is a function declaration
// The declared function name is signal
//signal Function has two parameters , The first is int type , The second is void(*)(int) The type of
//signal The return type of the function is still :void(*)(int) Function pointer type of
// Removing the function name and parameters is the return type , Such as int Add();int That is, the return type
// The above declaration code can be simplified into the following form :
pfun_t signal2(int,pfun_t);
}
7、 ... and 、 Function pointer array :
// Code 7:
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* arr[10];// An array of plastic pointers
// Function pointer array -- An array of function pointers
int (*pf1)(int,int) = Add;
int (*pf2)(int, int) = Sub;
int (*pf3)(int, int) = Mul;
int (*pf4)(int, int) = Div;
// Function pointer array --- First, an array
int (*pfArr[4])(int, int) = {Add,Sub,Mul,Div};
//pfArr[4] Is an array ,4 Elements , The type of each element is a int (*)(int, int)-- A function pointer
}
8、 ... and 、 Application of function pointer array : Calculator
// Code 8:
// Functions , Parameters , type , identical , So you can apply function pointer arrays
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;
}
void menu()
{
printf("1.add 2. sub 3. mul 4.div 0.exit\n");
}
int main()
{
int (*pfArr[5])(int, int) = {0,Add,Sub,Mul,Div};
// 0 1 2 3 4
int input = 0;
do
{
menu();
printf(" Please select \n");
scanf("%d",&input);
int x = 0;
int y = 0;
if (input == 0)
{
printf(" Exit calculator \n");
}
else if (input >= 1 && input <= 4)
{
printf(" Please enter two operands \n");
scanf("%d%d", &x, &y);
int ret = pfArr[input](x, y);
printf("%d\n",ret);
}
else
{
printf(" Please re-enter ");
}
} while (input);
return 0;
}
Nine 、 identify 3 A pointer to the :
1. Pointer to an integer array
2. Pointer to the array of integer pointers
3. A pointer to an array of function pointers
// Code 9:
// Example 1 :
int arr[10];// Shape array
int (*p)[10]= &arr;//int [10] ,p It's a pointer , Point to an array , Array has 10 Elements , The type of each element is int type
// therefore p Is a pointer to an integer array
// Example 2 :
int* arr[10];// Integer pointer array
int* (*p)[10] = &arr;// The address of the integer pointer array
//p It's a pointer , Point to an array , Array has 10 Elements , The type of each element is int* type
// therefore p It's a point ( int* [10]) Integer pointer array pointer
// Example 3 : A pointer to an array of function pointers
int Add(int x,int y)
{
return x + y;
}
int main()
{
int (*p)(int, int) = Add;//Add It represents the address of the function , The address of the function is received by the function pointer
int (*pA[5])(int, int);//pA Is an array of function pointers ,pA Is an array , There are function pointer types stored in the array
int(*(*pAA)[5])(int,int) = &pA;// The first thing to receive should be a pointer , This pointer points to an array , Every element in the array should be of function pointer type , Is to point to the above array with a pointer
//pAA Is a pointer to an array of function pointers
}
This paper sorts out the knowledge points of the pointer , Such as incorrect , Please comment in the comment area , thank
边栏推荐
- How will the complete NFT platform work in 2022? How about its core functions and online time?
- Alibaba cloud container service differentiation SLO hybrid technology practice
- A treasure open source software, cross platform terminal artifact tabby
- NPM script
- How to understand the gain bandwidth product operational amplifier gain
- Selenium library 4.5.0 keyword explanation (4)
- C # basic knowledge (3)
- Selenium library 4.5.0 keyword explanation (II)
- Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
- 2/14 (regular expression, sed streaming editor)
猜你喜欢
Alibaba cloud container service differentiation SLO hybrid technology practice
I wrote a chat software with timeout connect function
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
2/14 (regular expression, sed streaming editor)
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
The first game of the new year, many bug awards submitted
[note] IPC traditional interprocess communication and binder interprocess communication principle
Hcip 13th day notes
Hcip day 14 notes
随机推荐
JarPath
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Selenium library 4.5.0 keyword explanation (II)
Gossip about redis source code 81
Ramble 72 of redis source code
D25:sequence search (sequence search, translation + problem solving)
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
Tencent interview: can you find the number of 1 in binary?
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
Hcip day 16 notes
Unity shader visualizer shader graph
Gossip about redis source code 76
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Design of logic level conversion in high speed circuit
Selenium check box
Alibaba cloud container service differentiation SLO hybrid technology practice
Learning methods of zynq
Unsafe and CAS principle
Cgb2201 preparatory class evening self-study and lecture content