Array pointers and function pointers

Insert image description here

Welcome to follow the blogger Mindtechnist or join the [ Intelligent Technology Community ] to learn and share Linux, C, C++, Python, Matlab, robot motion control, multi-robot collaboration, intelligent optimization algorithms, filter estimation, multi-sensor information fusion, machine learning, artificial intelligence Knowledge and technology in intelligence and other related fields. Follow the public account "Machines and Intelligence" and reply with the keyword "python project practice" to get the video resources of Miduo Mall!


Blogger introduction:
CSDN quality creator, CSDN rising star, CSDN content partner;
Alibaba Cloud community expert blogger;
Huawei Cloud community cloud sharing expert;
51CTO community blogger, Nugget community blogger, Alipay community blogger , Blog Park blogger.



Column: "Proficient in C Language"


Preface

Array pointers and function pointers are both difficult knowledge points in C language, especially function pointers, and function pointers play a huge role in development.

Function pointer syntax

Define a function pointer and call the function indirectly through the function pointer:

int get_num(int a, int b)
{
    
    
    return a + b;
}

int (*func)(int a, int b); //定义了一个函数指针func,它指向 返回值为int 参数为 int a, int b的函数

func = &get_num; //函数指针指向函数

func(1, 2); //通过函数指针调用函数

Define a function pointer by defining a function pointer type:

typedef int (*func)(int a, int b); //定义了一个函数指针类型

func func1 = &get_num; //使用函数指针类型func定义函数指针变量func1

Define a function type and use the function type to define a function pointer

typedef int (func)(int a, int b); //定义了一个函数类型
func* func1 = &get_num;

func1(1, 1); //间接调用

The function pointer agrees on the return value and function parameters of the function. As long as the function developer implements the function function according to this agreement, he can call it through the unified interface with the function pointer as the parameter to use the function of the function, realizing function development and functionality. Use decoupling. This is the great role of function pointers: function pointers serve as function parameters
For a detailed explanation of the application of function pointers as function parameters, please see my other article:
callback function
and forward call usage scenarios of function pointers, such as loading the dynamic library into the program and finding the function entry address in the dynamic library. Call functions.

Array pointers and pointer arrays

An array pointer is a pointer to an array; an array of pointers is an array of pointers.

Array pointer example

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void PrintArray_int(int* array, int len)
{
    
    
	if ((NULL == array) || (len < 0))
	{
    
    
		printf("err:(NULL == array) || (len < 0)\n");
		return;
	}
	for (int i = 0; i < len; i++)
	{
    
    
		//两种方法访问数组元素
		//printf(" %d ", array[i]);
		printf(" %d ", *(array + i));
	}
	printf("\n");
}

int main()
{
    
    
	//定义一个数组变量
	int num_array[10];
	// num_array + 1 移动 sizeof(int) 个字节
	int len = sizeof(num_array) / sizeof(num_array[0]);
	//memset(num_array, 0, sizeof(num_array) / sizeof(num_array[0])); //典型错误,只把十个字节的内存值为0
	memset(num_array, 0, sizeof(num_array)); //实际应该把40个字节置为0

	PrintArray_int(num_array, sizeof(num_array) / sizeof(num_array[0]));

	//定义一个数组类型
	typedef int(array1_t)[];
	typedef int(array2_t)[10];

	array1_t a1 = {
    
    1, 2, 3};
	array2_t a2;
	for (int i = 0; i < 10; i++)
	{
    
    
		a2[i] = i;
	}
	PrintArray_int(a1, sizeof(a1) / sizeof(a1[0]));
	PrintArray_int(a2, 10);

	//定义一个数组指针
	int(*p_array)[10];
	// p_array + 1 移动 sizeof(int) * 10 个字节
	p_array = &num_array;
	for (int i = 0; i < 10; i++)
	{
    
    
		(*p_array)[i] = i + 1;
	}
	PrintArray_int(*p_array, 10);

	//定义一个指向数组的指针类型(数组指针)
	typedef int(*p_array_t)[10];
	p_array_t p1 = &num_array;
	for (int i = 0; i < 10; i++)
	{
    
    
		(*p1)[i] = i + 2; //先解引用,还原为一级指针(数组本身可以看为一级指针
						  //数组指针是指向数组的指针,也就是指向一级指针的指针,即二级指针)
	}
	PrintArray_int((*p1), 10);

	//指针数组
	const char* p2[2] = {
    
     "aaa", "bbb" };
	//p2[0] 和 p2[1] 都是一个指针
	printf("%s\n", p2[0]);
	printf("%s\n", p2[1]);

	system("pause");
	return 0;
}

おすすめ

転載: blog.csdn.net/qq_43471489/article/details/134866757