Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与C99驱动程序接口的问题_C_Function_Pointers_Struct - Fatal编程技术网

与C99驱动程序接口的问题

与C99驱动程序接口的问题,c,function,pointers,struct,C,Function,Pointers,Struct,我正在编写代码,以便将外围设备与MCU开发板连接起来。外围设备配备了可供使用的驱动程序。我有以下表格: 在d_base.h中: struct S1 { void (*funcptr)(int x, int y); }; 在driver.c中: #include "d_base.h" #include "driver.h" static struct S1 const* ptr; 在driver.h中,有作为外围设备驱动程序一部分的预写功能。每当需要特定功能时,这些功能都会引用ptr-

我正在编写代码,以便将外围设备与MCU开发板连接起来。外围设备配备了可供使用的驱动程序。我有以下表格:

在d_base.h中:

struct S1 {
   void (*funcptr)(int x, int y);
};
在driver.c中:

#include "d_base.h"
#include "driver.h"

static struct S1 const* ptr;
在driver.h中,有作为外围设备驱动程序一部分的预写功能。每当需要特定功能时,这些功能都会引用
ptr->funcptr
。例如:

#include "d_base.h"

ptr->funcptr(x,y);
函数的实现由我自己决定,因为它与我正在使用的硬件有关

然后我在main.c中编写了一个函数,如下所示:

#include "driver.h"

void fun(int x, int y)
{
      // do nothing
}
我的问题是将函数
ptr->funcptr
链接到
fun
。当调用ptr->funcptr时,如何使编译器进入函数乐趣


谢谢您的帮助。

您可能有一个
结构S1
,您可以在那里分配
乐趣

#include <stdio.h>
#include <stdlib.h>

struct S1{
 void (*funcptr)(int,int);
};

static struct S1 const* ptr;

void fun(int x, int y){
  printf("Hello");
}

int main(){
  struct S1 s;
  s.funcptr = fun;
  ptr=&s;
  ptr->funcptr(0,0);
  }
#包括
#包括
结构S1{
无效(*funcptr)(整数,整数);
};
静态结构S1 const*ptr;
虚无乐趣(整数x,整数y){
printf(“你好”);
}
int main(){
结构s1s;
s、 funcptr=乐趣;
ptr=&s;
ptr->funcptr(0,0);
}

在这种情况下,您无法访问结构S1,可能存在动态生成它的API函数。

您可能有一个结构S1,您可以在其中分配
乐趣

#include <stdio.h>
#include <stdlib.h>

struct S1{
 void (*funcptr)(int,int);
};

static struct S1 const* ptr;

void fun(int x, int y){
  printf("Hello");
}

int main(){
  struct S1 s;
  s.funcptr = fun;
  ptr=&s;
  ptr->funcptr(0,0);
  }
#包括
#包括
结构S1{
无效(*funcptr)(整数,整数);
};
静态结构S1 const*ptr;
虚无乐趣(整数x,整数y){
printf(“你好”);
}
int main(){
结构s1s;
s、 funcptr=乐趣;
ptr=&s;
ptr->funcptr(0,0);
}

在这里,你不能访问<代码>结构> S1<代码>,也许有API函数动态生成。< /P>为什么添加C++标签?这是一种无关的语言。不要垃圾邮件标签!对于MCU驱动程序来说,这看起来很糟糕。这引起了对代码其余部分总体质量的怀疑。对于不必要的标记,我感到非常抱歉。上面的行是
static struct S1 const*ptr代码中的实际行(即复制和粘贴的行),或者您是否重新键入该行,并且可能在过程中出错?我询问的原因是,如果这确实是正确的声明,则不能通过
ptr
修改
funcptr
。这是一种复制粘贴。我刚刚更改了S1和PTR名称标签。为什么添加C++标签?这是一种无关的语言。不要垃圾邮件标签!对于MCU驱动程序来说,这看起来很糟糕。这引起了对代码其余部分总体质量的怀疑。对于不必要的标记,我感到非常抱歉。上面的行是
static struct S1 const*ptr代码中的实际行(即复制和粘贴的行),或者您是否重新键入该行,并且可能在过程中出错?我询问的原因是,如果这确实是正确的声明,则不能通过
ptr
修改
funcptr
。这是一种复制粘贴。我刚换了S1和ptr的标签。谢谢你的建议。在此之后,进一步分析它最终是正确连接的。最终的设置如下:#include#include struct S1{void(funcptr)(int,int);};静态结构S1 const ptr;结构S1 const*sfun;静态结构s1s;void-fun(intx,inty){printf(“Hello”);}void-init(struct S1 const*sfun){ptr=sfun;}int-main(){s.funcptr=fun;init(&s);ptr->funcptr(0,0);}谢谢您的建议。在此之后,进一步分析它最终是正确连接的。最终的设置如下:#include#include struct S1{void(funcptr)(int,int);};静态结构S1 const ptr;结构S1 const*sfun;静态结构s1s;void-fun(intx,inty){printf(“Hello”);}void-init(struct S1 const*sfun){ptr=sfun;}int-main(){s.funcptr=fun;init(&s);ptr->funcptr(0,0);}