Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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_Function Pointers - Fatal编程技术网

C中的数据类型,表示指向函数的任何指针

C中的数据类型,表示指向函数的任何指针,c,function-pointers,C,Function Pointers,假设我对函数指针有以下定义: typedef void(*Tmp112HardwareInit)(); typedef void(*Tmp112HardwareDeInit)(); typedef bool(*Tmp112HardwareIsBusy)(); typedef struct { Tmp112HardwareInit init; Tmp112HardwareDeInit deinit; Tmp112HardwareIsBusy is_busy; } I2CI

假设我对函数指针有以下定义:

typedef void(*Tmp112HardwareInit)();
typedef void(*Tmp112HardwareDeInit)();
typedef bool(*Tmp112HardwareIsBusy)();
typedef struct
{
    Tmp112HardwareInit init;
    Tmp112HardwareDeInit deinit;
    Tmp112HardwareIsBusy is_busy;
} I2CInterface;

static I2CInterface _i2c_interface; // This is declared in module scope
// Generic function
bool _SensorTMP112_InterfaceOperation(UnknownType fptr) // What should be UnknownType?
{
    bool success = true;
    if (fptr)
    {
        fptr();
    }
    else
    {
        success = false;
    }

    return success;
}

// Operation specific functions
bool _SensorTMP112_InterfaceInit()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.init);
}

bool _SensorTMP112_InterfaceDeInit()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.deinit);
}

bool _SensorTMP112_InterfaceIsBusy()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.is_busy);
}
我有一个接口对象来保存上述函数指针的实例:

typedef void(*Tmp112HardwareInit)();
typedef void(*Tmp112HardwareDeInit)();
typedef bool(*Tmp112HardwareIsBusy)();
typedef struct
{
    Tmp112HardwareInit init;
    Tmp112HardwareDeInit deinit;
    Tmp112HardwareIsBusy is_busy;
} I2CInterface;

static I2CInterface _i2c_interface; // This is declared in module scope
// Generic function
bool _SensorTMP112_InterfaceOperation(UnknownType fptr) // What should be UnknownType?
{
    bool success = true;
    if (fptr)
    {
        fptr();
    }
    else
    {
        success = false;
    }

    return success;
}

// Operation specific functions
bool _SensorTMP112_InterfaceInit()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.init);
}

bool _SensorTMP112_InterfaceDeInit()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.deinit);
}

bool _SensorTMP112_InterfaceIsBusy()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.is_busy);
}
在访问这些函数之前,我想检查它们是否被分配了某些内容。所以不是

_i2c_interface.init();
我想要

if (_i2c_interface.init) _i2c_interface.init();
else error = true;
我不想对
\u i2c\u接口
中每个变量的每次访问都重复这些行,因此我想编写一个函数:

bool _SensorTMP112_InterfaceInit()
{
    bool success = true;
    if (_i2c_interface.init)
    {
        _i2c_interface.init();
    }
    else
    {
        success = false;
    }

    return success;
}
现在,我不想为
\uI2C\u接口
变量中的每个函数指针编写函数。我想要一个泛型函数,其他函数将为每个函数指针调用该函数:

typedef void(*Tmp112HardwareInit)();
typedef void(*Tmp112HardwareDeInit)();
typedef bool(*Tmp112HardwareIsBusy)();
typedef struct
{
    Tmp112HardwareInit init;
    Tmp112HardwareDeInit deinit;
    Tmp112HardwareIsBusy is_busy;
} I2CInterface;

static I2CInterface _i2c_interface; // This is declared in module scope
// Generic function
bool _SensorTMP112_InterfaceOperation(UnknownType fptr) // What should be UnknownType?
{
    bool success = true;
    if (fptr)
    {
        fptr();
    }
    else
    {
        success = false;
    }

    return success;
}

// Operation specific functions
bool _SensorTMP112_InterfaceInit()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.init);
}

bool _SensorTMP112_InterfaceDeInit()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.deinit);
}

bool _SensorTMP112_InterfaceIsBusy()
{
    return _SensorTMP112_InterfaceOperation(_i2c_interface.is_busy);
}

我的问题是
未知类型应该是什么类型?或者在标准C中也有可能吗?或者,对于我想要实现的目标,还有其他方法吗?对非标准解决方案不感兴趣。

Quick'n'dirty预处理器宏:

#define callfn(f) if(f) f(); else result = false
然后

(当前面定义了
bool success
时),或在函数中使用它:

bool _SensorTMP112_InterfaceInit()
{
    bool success = true;
    callfn(_i2c_interface.init);
    return success;
}
(也可以将
bool success
return success
嵌入宏中)


但是您不应该公开该宏(即在调用它的函数体之后的
#unde callfn
),因为它会留下许多打开的门。

快速脏的预处理器宏:

#define callfn(f) if(f) f(); else result = false
然后

(当前面定义了
bool success
时),或在函数中使用它:

bool _SensorTMP112_InterfaceInit()
{
    bool success = true;
    callfn(_i2c_interface.init);
    return success;
}
(也可以将
bool success
return success
嵌入宏中)


但是您不应该公开该宏(即在调用它的函数体之后的
#unde callfn
),因为它会留下许多打开的门。

您希望如何处理具有不同数量和/或参数类型的函数?有不同返回类型的函数呢?在一个不相关的注释中,以一个下划线开头,后跟一个大写字母的名称(例如
\u SensorTMP112\u interface init
)由C规范保留,您不应该自己声明或定义这些名称。例如,请参见规范中的报价。它还告诉您,在函数(您似乎称之为“模块”作用域)之外,不能使用任何以下划线开头的符号。预处理器宏对此很好。@dbush:good catch。还没想过呢。@someprogrammer都德:谢谢。不知道。您希望如何处理具有不同数量和/或参数类型的函数?有不同返回类型的函数呢?在一个不相关的注释中,以一个下划线开头,后跟一个大写字母的名称(例如
\u SensorTMP112\u interface init
)由C规范保留,您不应该自己声明或定义这些名称。例如,请参见规范中的报价。它还告诉您,在函数(您似乎称之为“模块”作用域)之外,不能使用任何以下划线开头的符号。预处理器宏对此很好。@dbush:good catch。还没想过呢。@someprogrammer都德:谢谢。我不知道。