confusing concept

1. Array of structures

Each element of the array is a structure, structure array format: declare variable type array name [array length]

2. Function pointer

The function pointer is essentially a pointer , and the address pointed to by the pointer is a function, so it is a pointer to the function.
In C language, the definition of the function is stored in the code segment, and each function has an entry address in the code segment, and the function pointer points to A pointer to the entry address of a function in the code segment
Declaration form: ret (*p)(args, *argv);
Remarks:

  1. Function pointers cannot be used for addition and subtraction
  2. The brackets on both sides of the form (*pointer variable name) are indispensable, where * is a symbol, not an evaluation operation

2.1 Initialization of function pointers

The function pointer needs to assign the address of a function to it. The function name is the entry address of the function. Therefore, the function name is the function pointer of the function, so the initialization of the function pointer can be written as follows:函数指针变量 = 函数名;

2.2 Application of function pointer: callback function

A callback function is a function called through a pointer function . The callback function passes the function pointer as a parameter to another function. The callback function is not called by the function designer, but by another party when a specific event or condition occurs.

3. Pointer functions

A pointer function is essentially a function , but its return value is a pointer
Declaration form: ret *func(args, *argv);
among them, * can be close to ret, can be close to func, or can be in between, generally choose close to ret, so it looks more like a pointer
Remarks:
When using pointer functions, it is necessary to avoid the situation of returning local variable pointers, because local variables are stored in the stack area. When the function ends, the variables in the stack area will be released. If a variable is defined inside the function, Then use a pointer to point to this variable. When the function call ends, the space of this variable has been released. If the function return value is a pointer that returns the address, then the program may not .

Fourth, the function pointer variable

Function pointer variable calling steps:

  1. First define the function pointer variable, defined as a function pointer variable
  2. Assign the entry address (function name) of the called function to the function pointer variable
  3. Use the function pointer variable to call the function, the calling form is: (*pointer variable name) (actual parameter list)

5. Array of function pointers

A function pointer array is an array that stores multiple function pointers. The function pointer array can effectively reduce the cyclomatic complexity.
For example: int (*p1)[32](int, int);
Remarks:

  1. The return type of the function is the same, and the formal parameters of the function must also be the same
  2. Using an array of function pointers in the switch function can simplify the code

6. Array pointer

Array pointer: the essence is a pointer, which is a pointer to an array
Form: (*p)[n]
for example:

#include "stdio.h"

int main()
{
    
    
	int a[5] = {
    
     1, 2, 3, 4, 5 };	// a 数组里有 5 个元素
	int (*p)[5];	//定义一个数组指针 p
	p = &a;			//把数组a的地址赋给p,故 p 为数组 a 的地址,*p 表示数组 a 本身
	
	printf("%p\n", a); 			//打印数组名
	printf("%p\n", p); 			//打印数组 a 的地址
	printf("%p\n", *p); 		//打印数组的首元素地址
	printf("%p\n", &a[0]); 		//打印a[0]的地址
	printf("%p\n", &a[1]); 		//打印a[1]的地址
	printf("%p\n", p[0]); 		//打印数组首元素的地址
	printf("%d\n", **p); 		//打印首元素的值
	printf("%d\n", *p[0]); 		//打印首元素的值
	
    return 0;
}

Seven, pointer array

Array of pointers: the essence is an array, and the elements of the array are pointers
Form: *p[n]
for example:

#include "stdio.h"

int main()
{
    
    
	int a = 1;
	int b = 2;
	int *p[2];
	p[0] = &a;
	p[1] = &b;
 
	printf("%p\n", p[0]); 		//打印a的地址
	printf("%p\n", &a); 		//打印a的地址
	printf("%p\n", p[1]); 		//打印b的地址
	printf("%p\n", &b); 		//打印b的地址
	printf("%d\n", *p[0]); 		//p[0]表示a的地址,*p[0]表示打印a的值
	printf("%d\n", *p[1]); 		//p[1]表示b的地址,*p[1]表示打印b的值
 
    return 0;
}

Remark:

  1. The array pointer is a pointer variable that occupies the storage space of a pointer in the memory. In a 32-bit system, the pointer occupies four bytes.
  2. The pointer array is a plurality of pointer variables, stored in the memory in the form of an array, occupying the storage space of multiple pointers

Guess you like

Origin blog.csdn.net/future_sky_word/article/details/125649042