【发布时间】:2016-11-16 14:34:06
【问题描述】:
我有一个程序将调用库中存在的函数。函数的参数是一个函数指针。
Helloworld.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "helloworld.h"
struct data_buffer
{
char name[10];
}data;
int main()
{
int result;
int (*func_pointer)(&data_buffer); //function pointer which takes structure as parameter
result=send_data(func_ponter); //error
func_pointer(&data_buffer); //call the SPI write
}
helloworld.h
#ifndef HELLOWORLD_H
#define HELLOWORLD_H
/* Some cross-platform definitions generated by autotools */
#if HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
/*
* Example function
*/
struct data_buffer;
extern int send_data(int (*func_pointer)(&data_buffer)); //is the declaration correct
#endif
libexample.c
#include<stdio.h>
#include<stdlib.h>
int send_data(int (*func_pointer)(&data_buffer)) //error
{
func_pointer=spi_write(); // assigning the function pointer to SPI WRITE function
return;
}
所以我的项目目标是将函数指针作为参数发送到 send_data 函数。在库程序中,必须将函数指针分配给 spi_write() 函数,然后才能在 Helloworld 程序中借助函数指针调用 SPI_Write。
【问题讨论】:
-
不太清楚您要达到的目标。将
spi_write分配给参数会完成什么?什么是spi_write? (另外,当你写x = y;时,你是在“将 y 分配给 x”,而不是相反。)
标签: c pointers shared-libraries function-pointers