当前位置:网站首页>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
边栏推荐
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- Live app source code, jump to links outside the station or jump to pages inside the platform
- Hcip day 14 notes
- Idea set class header comments
- Introduction to the gtid mode of MySQL master-slave replication
- Smart fan system based on stm32f407
- A treasure open source software, cross platform terminal artifact tabby
- C # basic knowledge (1)
- 在恒泰证券开户怎么样?安全吗?
- 2022.02.14
猜你喜欢
Pytorch learning notes 5: model creation
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)
Interesting 10 CMD commands
leetcode-43. String multiplication
Idea a method for starting multiple instances of a service
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Cgb2201 preparatory class evening self-study and lecture content
The interviewer's biggest lie to deceive you, bypassing three years of less struggle
Tencent interview: can you find the number of 1 in binary?
How to make recv have a little temper?
随机推荐
How to quickly build high availability of service discovery
Hcip day 16 notes
leetcode-43. String multiplication
China standard gas market prospect investment and development feasibility study report 2022-2028
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
股票開戶傭金最低的券商有哪些大家推薦一下,手機上開戶安全嗎
Introduction to the gtid mode of MySQL master-slave replication
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
C summary of knowledge point definitions, summary notes
Alibaba cloud container service differentiation SLO hybrid technology practice
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
Idea set class header comments
How to understand the gain bandwidth product operational amplifier gain
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Comment obtenir une commission préférentielle pour l'ouverture d'un compte en bourse? Est - ce que l'ouverture d'un compte en ligne est sécurisée?
Selenium library 4.5.0 keyword explanation (I)
Zipper table in data warehouse (compressed storage)
Selenium library 4.5.0 keyword explanation (II)
Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii