这段代码是怎么执行?

dsoyy 2013-09-27 01:29:07
有这样的定义
定义:
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
);


函数这样被调用:
(sio_device_head[port_id].HW_ioctl) (strwam_id,port_id,cmd,param);

我不清楚这个函数的执行过程,请指点。

...全文
188 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
一根烂笔头 2013-09-27
  • 打赏
  • 举报
回复
在某处应该有类似下面的操作,把函数赋值给具体的函数指针。

sio_device_head[port_id].HW_ioctl = XXX_ioctl;
那么在具体调用的时候,就直接使用函数指针操作即可!
hu7324829 2013-09-27
  • 打赏
  • 举报
回复
(sio_device_head[port_id].HW_ioctl) (strwam_id,port_id,cmd,param); 数组sio_device_head的第port_id个元素里有个函数指针HW_ioctl, 函数指针类型就是你上面的定义
赵4老师 2013-09-27
  • 打赏
  • 举报
回复
#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
赵4老师 2013-09-27
  • 打赏
  • 举报
回复
#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
//
halleyzhang3 2013-09-27
  • 打赏
  • 举报
回复
是个函数指针,肯定有赋值的地方
wb23222 2013-09-27
  • 打赏
  • 举报
回复
引用 4 楼 dsoyy 的回复:
(sio_device_head[port_id].HW_ioctl) (strwam_id,port_id,cmd,param); 我能看出前面是一个指针,指向一个函数,后面是这个函数的参数。 但是这个函数的原型在哪,我不知道怎么去找。
定义就是原型被~~~
wb23222 2013-09-27
  • 打赏
  • 举报
回复
引用 3 楼 qinken547 的回复:
函数指针,sio_device_head[port_id].HW_ioctl返回的是一个函数指针,后面括号里是参数
定义就是原型被~~~
qinken547 2013-09-27
  • 打赏
  • 举报
回复
形如void f(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是如何被赋值的吧
dsoyy 2013-09-27
  • 打赏
  • 举报
回复
(sio_device_head[port_id].HW_ioctl) (strwam_id,port_id,cmd,param); 我能看出前面是一个指针,指向一个函数,后面是这个函数的参数。 但是这个函数的原型在哪,我不知道怎么去找。
qinken547 2013-09-27
  • 打赏
  • 举报
回复
函数指针,sio_device_head[port_id].HW_ioctl返回的是一个函数指针,后面括号里是参数
赵4老师 2013-09-27
  • 打赏
  • 举报
回复
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*/
  • 打赏
  • 举报
回复
我也不清楚啊

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧