69,382
社区成员
发帖
与我相关
我的任务
分享
sio_device_head[port_id].HW_ioctl = XXX_ioctl;
那么在具体调用的时候,就直接使用函数指针操作即可!#include <stdio.h>
double A[300][100];
double valuex[300];
double valuey[300];
int i;
double fun00(double x,double y) {return x + y ;};
double fun01(double x,double y) {return x*x+ 3*y ;};
double fun02(double x,double y) {return x + 2*y*x;};
//... fun03(double x,double y) {return ...+... ;};
//...
//... fun98(double x,double y) {return ...+... ;};
double fun99(double x,double y) {return x/2+20*y ;};
double (*funNN[100])(double,double)={
fun00,
fun01,
fun02,
// fun03,
// ...
// fun98,
fun99,
};
int main() {
double x,y;
int f,FN;
for (i=0;i<300;i++) {
valuex[i]=(double)i;
valuey[i]=(double)i;
}
FN=3;
for (i=0;i<300;i++) {
x=valuex[i];
y=valuey[i];
for (f=0;f<FN;f++) A[i][f]=funNN[f](x,y);
}
for (i=0;i<3;i++) {
for (f=0;f<FN;f++) printf("%lg ",A[i][f]);
printf("\n");
}
return 0;
}
//0 0 0
//2 4 3
//4 10 10
// 假设有一个函数指针
// void (*a)(char*);
// 有一个函数
// void b(int n)
// {
// }
// 有没有办法实现执行a("10")时转换成执行b(10)?
#include <stdio.h>
void (*a)(char*);
void b(int n)
{
printf("in b:n==%d\n",n);
}
void A(char *pA)
{
int pB;
sscanf(pA,"%d",&pB);
b(pB);
}
int main() {
a=A;
a("10");
}
//in b:n==10
#include <stdio.h>
typedef int sio_stream_id_type;
typedef int sio_port_id_type;
typedef int sio_ioctl_cmd_type;
typedef int sio_ioctl_param_type;
struct X {
int k;
void (*HW_ioctl)(sio_stream_id_type stream_id,
sio_port_id_type port_id ,
sio_ioctl_cmd_type cmd ,
sio_ioctl_param_type param
);
/*
定义一个指针变量HW_ioctl,
该变量的类型是指向一类函数的指针。
这类函数无返回值,有4个参数,其类型依次为
sio_stream_id_type,
sio_port_id_type,
sio_ioctl_cmd_type,
sio_ioctl_param_type
*/
} sio_device_head[2];//结构体数组sio_device_head
sio_stream_id_type strwam_id;
sio_port_id_type port_id ;
sio_ioctl_cmd_type cmd ;
sio_ioctl_param_type param ;
void fun123(sio_stream_id_type stream_id,
sio_port_id_type port_id ,
sio_ioctl_cmd_type cmd ,
sio_ioctl_param_type param
) {
printf("in fun123\n");
}
void fun456(sio_stream_id_type stream_id,
sio_port_id_type port_id ,
sio_ioctl_cmd_type cmd ,
sio_ioctl_param_type param
) {
printf("in fun456\n");
}
int main() {
sio_device_head[0].k=123;
sio_device_head[0].HW_ioctl=fun123;
sio_device_head[1].k=456;
sio_device_head[1].HW_ioctl=fun456;
for (port_id=0;port_id<2;port_id++) {
(sio_device_head[port_id].HW_ioctl)(strwam_id,port_id,cmd,param);
/*调用“结构体数组sio_device_head的第port_id项对应的
结构体的成员变量HW_ioctl指向的”函数,4个参数依次为strwam_id,port_id,cmd,param
*/
}
return 0;
}
//in fun123
//in fun456
//
void (*HW_ioctl)(sio_stream_id_type stream_id,
sio_port_id_type port_id,
sio_ioctl_cmd_type cmd,
sio_ioctl_param_type param
);
/*
HW_ioctl是指向一类函数的指针,这类函数无返回值,4个参数类型依次为
sio_stream_id_type stream_id,
sio_port_id_type port_id,
sio_ioctl_cmd_type cmd,
sio_ioctl_param_type param
*/
(sio_device_head[port_id].HW_ioctl) (strwam_id,port_id,cmd,param);
/*调用“结构体数组sio_device_head的第port_id项对应的结构体的成员HW_ioctl指向的”函数,4个参数依次为strwam_id,port_id,cmd,param*/