Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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
C 每次结构调用使用一次指向结构的函数指针_C_Syntax - Fatal编程技术网

C 每次结构调用使用一次指向结构的函数指针

C 每次结构调用使用一次指向结构的函数指针,c,syntax,C,Syntax,我有一个包含几个函数指针的结构。通用接口在头文件中创建 头文件 typedef struct { void (*Start)(void); void (*ByteWrite)(uint8_t *pBuffer); // Modifies I2C buffer uint8_t (*ByteRead)(uint8_t *pBuffer); void (*ArrayWrite)(uint8_t *pBuffer); uint8_t (*ArrayR

我有一个包含几个函数指针的结构。通用接口在头文件中创建

头文件

typedef struct
{
    void (*Start)(void);
    void (*ByteWrite)(uint8_t *pBuffer);        // Modifies I2C buffer
    uint8_t (*ByteRead)(uint8_t *pBuffer);
    void (*ArrayWrite)(uint8_t *pBuffer);
    uint8_t (*ArrayRead)(uint8_t *pBuffer);
    bool (*Busy)(void);
} sI2C_t;


extern const sI2C_t I2C0;
extern const sI2C_t I2C1;
extern const sI2C_t I2C2;
static void I2C0_Start(void) { ... }
static void I2C0_ByteWrite(*uint8_t) { ... }
static uint8_t I2C0_ByteRead(*uint8_t) { ... }
static void I2C0_ArrayWrite(*uint8_t) { ... }
static uint8_t I2C_ArrayRead(*uint8_t) { ... }
static bool I2C_Busy(void) { ... }

const sI2C I2C0 =
{
    I2C0_Start,
    I2C0_ByteWrite,
    I2C0_ByteRead,
    I2C0_ArrayWrite,
    I2C0_ArrayRead,
    I2C0_Busy
};

// Code-block repeated for I2C1, I2C2, etc. (REDUNDANT!)
然后在C文件中实现每个函数指针以满足结构接口

C文件

typedef struct
{
    void (*Start)(void);
    void (*ByteWrite)(uint8_t *pBuffer);        // Modifies I2C buffer
    uint8_t (*ByteRead)(uint8_t *pBuffer);
    void (*ArrayWrite)(uint8_t *pBuffer);
    uint8_t (*ArrayRead)(uint8_t *pBuffer);
    bool (*Busy)(void);
} sI2C_t;


extern const sI2C_t I2C0;
extern const sI2C_t I2C1;
extern const sI2C_t I2C2;
static void I2C0_Start(void) { ... }
static void I2C0_ByteWrite(*uint8_t) { ... }
static uint8_t I2C0_ByteRead(*uint8_t) { ... }
static void I2C0_ArrayWrite(*uint8_t) { ... }
static uint8_t I2C_ArrayRead(*uint8_t) { ... }
static bool I2C_Busy(void) { ... }

const sI2C I2C0 =
{
    I2C0_Start,
    I2C0_ByteWrite,
    I2C0_ByteRead,
    I2C0_ArrayWrite,
    I2C0_ArrayRead,
    I2C0_Busy
};

// Code-block repeated for I2C1, I2C2, etc. (REDUNDANT!)
这使得访问特定于I2C接口的功能相对容易:

bool status;

I2C0.Start();
status = I2C1.Busy();
...

虽然I2C0、I2C1和I2C2等的函数指针基本相同,但我必须为每个新的结构接口分别写出它们。由于这是多余的,有没有办法让我只实现一次这些函数指针?

您可以编写一个构造函数。例如:

typedef struct{
     int    a;
     char   b;
}example;

void constructor (example *pointer_to_struct, int a_value, char b_value){
    pointer_to_struct->a = a_value;
    pointer_to_struct->b = b_value;   /*remember: if you have strings don't have any 
                                     assignments, since a string (like any other array) is a pointer to 
                                     its first element*/
}


int main (void){

    example ex_struct;
    constructor(&ex_struct, 10, 'C');

    return 0;
}
void constructor(structure *p){
     p->a = 10;
     p->b = 'C';
}
编辑:您还可以编写一个函数,为所选类型的每个结构进行相同的赋值。例如:

typedef struct{
     int    a;
     char   b;
}example;

void constructor (example *pointer_to_struct, int a_value, char b_value){
    pointer_to_struct->a = a_value;
    pointer_to_struct->b = b_value;   /*remember: if you have strings don't have any 
                                     assignments, since a string (like any other array) is a pointer to 
                                     its first element*/
}


int main (void){

    example ex_struct;
    constructor(&ex_struct, 10, 'C');

    return 0;
}
void constructor(structure *p){
     p->a = 10;
     p->b = 'C';
}

标准解决方案是将结构指针作为第一个参数传递给函数。即代替:

I2C0.Start();
你写道:

I2C0.Start(&I2C0);
然后,您可以向结构中添加一些额外字段,以确定它是哪一个(例如,如果每个I2C总线都有固定的硬件地址,则可能在结构的一个额外字段中有硬件地址)


这是一个普通的C++方法,相当于C++类。

这可能是其中一个宏是一个合理的解决方案的场景之一。这就是说,如果将
I2C0
I2C1
等作为变量名,那么从总体上看,数组可能是一种更好的方法??如
外部常量sI2C\t I2C[MAX\u NUM]?仍然需要为每个函数定义函数,对吗?你能举一个我如何使用宏的例子吗?救命啊!X宏允许我区分I2C0/1/2吗?在函数指针中,我需要预料到这一点。
*uint8\u t
?您确定可以在参数列表中取消引用类型名吗?我确实想过将其作为指针传入,但在我看来,因为我区分了
I2C0/1/2
(例如
I2C0.Start()
I2C1.Start()
),所以可以隐式指示它是哪一个。