c 函数指针

int   inc(int   a)
{
	return(++a);
}

int   multi(int*a, int*b, int*c)
{
	//11 10 0
	return(*c = *a**b);
}

typedef   int(FUNC1)(int);
typedef   int(FUNC2)(int*, int*, int*);

void   show(FUNC2   fun, int   arg1, int*arg2)
{
	FUNC1   *p = &inc;
	int   temp = p(arg1);
	fun(&temp, &arg1, arg2);
	printf("%d\n ", *arg2);
}

main()
{
	int   a;
	show(multi, 10, &a);
	getchar();
	return   0;
}

基础实例:


#include "stdio.h"
#include "stdlib.h"
#include "string.h"


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

//定义一个指向数组的 数组指针类型 
typedef int (*PArrayType)[10];  // int *

void main_数组类型复习()
{
	//由数组类型定义变量
	ArrayType array; //int array2[10];
	array[0] = 1;

	{
		//用数组指针类型  去定义一个指针 
		PArrayType arrayp; //定义一个指向数组类型的指针
		arrayp = &array;
		(*arrayp)[0] = 2;
	}

	{
		//直接定义一个指向数组类型的指针
		int (*myParray)[10] =  &array;
			(*myParray)[0] = 3;
	}

	printf("hello...\n");
	system("pause");
}

//定义一个函数类型 
typedef int Func(int);

//函数名称就代表函数的入口地址 函数名称本身就是一个指针
int test(int a)
{
	return a*a;
}



void main12()
{
	//用函数类型 定义一个函数指针
	Func *myfun = NULL;
	myfun = test;

	//通过函数指针(函数的入口地址)可以指向函数体。(言外之意可以进行函数调用)
	//printf("%d \n", myfun(2));

	//对函数名取多少地址 都是一样
	myfun = &test;
	printf("%d \n", myfun(2));

	//通过通过函数指针可以执行一个函数调用

	//(*(*(test)));

	system("pause");
}

void f()
{
	printf("执行了f\n");
}
void main()
{
	//直接定义一个函数指针,并且赋值 
	void (*myf1)() = f;
	void (*myf2)() = &f;

	myf2();
	myf1();

	system("pause");
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值