C语言第21天,指针(四),函数指针 | 函数指针数组

函数指针

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

//函数指针

int Add(int x, int y) {
	int z = 0;
	z = x + y;
	return z;
}

void Print(char* str) {
	printf("%s\n", str);
}

int main() {
	int a = 10;
	int b = 20;
	int arr[10] = { 0 };
	int(*p)[10] = &arr;

	printf("%p\n", &Add);
	printf("%p\n", Add);
	//&函数名 和 函数名 都是函数的地址

	printf("----------------\n");

	int(*pa)(int, int) = Add;
	printf("%d\n", (*pa)(2, 3));//5
	printf("%d\n", pa(2, 3));//5
	printf("%d\n", Add(2, 3));//5

	void(*pc)(char*) = Print;
	(*pc)("hello world");

	return 0;
}
001510B4
001510B4
----------------
5
5
5
hello world

函数指针数组(一)

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

//函数指针数组

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[5];
	int(*pa)(int, int) = Add;

	//需要一个数组,这个数组可以存放多个函数的地址 - 函数指针的数组
	int(*parr[4])(int, int) = { Add,Sub,Mul,Div };//函数指针的数组
	int i = 0;
	for (i = 0; i < 4; i++) {
		printf("%d\n", parr[i](2, 3));
	}
	return 0;
}
5
-1
6
0

函数指针数组(二)

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

char* my_strcpy(char* dest, const char* src);

int main() {
	//1.写一个函数指针 pf ,能够指向my_strcpy
	char* (*pf)(char*, const char*);
	//2.写一个函数指针数组 pfArr,能够存放4个my_strcpy函数的地址
	char* (*pfArr[4])(char*, const char*);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值