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
C 函数指针的结构_C_Pointers_Struct - Fatal编程技术网

C 函数指针的结构

C 函数指针的结构,c,pointers,struct,C,Pointers,Struct,如果我有一个结构,它有一个指向这样一个函数的指针 struct str{ int some_element; char other_element; int (*my_pointer_to_a_function)(int); }; struct str my_struct; int my_function(int); 我给它赋值 my_struct.some_element = 1; my_struct.other_element = 'a'; my_struct.my_pointer

如果我有一个结构,它有一个指向这样一个函数的指针

struct str{
 int some_element;
 char other_element;
 int (*my_pointer_to_a_function)(int);
};

struct str my_struct;
int my_function(int);
我给它赋值

my_struct.some_element = 1;
my_struct.other_element = 'a';
my_struct.my_pointer_to_a_function = my_function;
如何调用指针指向的函数(使用指针)? 我的初步猜测是:

my_struct.(*my_pointer_to_a_function)(value);
还是应该如此

 *my_struct.my_pointer_to_a_function(value);
?


谢谢。

假设您有指向函数的指针,就像您的结构成员一样:

    struct newtype{
        int a;
        char c;
        int (*f)(struct newtype*);
    } var;
    int fun(struct newtype* v){
        return v->a;
    }
你可以这样称呼它:

    int main(){
        var.f=fun;
        var.f(&var);
     //  ^.....^..... have to pass `var` as an argument to f() :( :(
    }

 //Comment: here in var.f(&var); I miss this pointer and C++,      
因此,对于您的情况,它应该是
my_struct.my_pointer_to_a_函数(value)

其他要点:
在我的示例中需要注意的重要一点是,即使您希望访问同一结构变量的成员,您也必须传递它。(与C++对象完全不同) C++类中的虚拟函数。它们在引擎盖下以类似的方式实现


下面是一个可以帮助您使用的项目:

假设您有指向函数的指针,就像您的结构成员一样:

    struct newtype{
        int a;
        char c;
        int (*f)(struct newtype*);
    } var;
    int fun(struct newtype* v){
        return v->a;
    }
你可以这样称呼它:

    int main(){
        var.f=fun;
        var.f(&var);
     //  ^.....^..... have to pass `var` as an argument to f() :( :(
    }

 //Comment: here in var.f(&var); I miss this pointer and C++,      
因此,对于您的情况,它应该是
my_struct.my_pointer_to_a_函数(value)

其他要点:
在我的示例中需要注意的重要一点是,即使您希望访问同一结构变量的成员,您也必须传递它。(与C++对象完全不同) C++类中的虚拟函数。它们在引擎盖下以类似的方式实现


下面是一个帮助您使用的项目:

指向函数的指针可以按原样使用,无需任何取消引用:

my_struct.my_pointer_to_a_function(value)
但如果您坚持要取消引用,则必须以这种方式使用括号:

(*my_struct.my_pointer_to_a_function)(value)
两者完全相同,所以我推荐第一种,更简单

关于您第一次尝试:

my_struct.(*my_pointer_to_a_function)(value); //Error!
这是行不通的,因为必须首先计算parenthersis中的表达式:
*my\u pointer\u to\u a\u function
,但这本身并不意味着什么

还有你的第二个:

*my_struct.my_pointer_to_a_function(value); //Error!
运算符优先级规则首先计算
,然后计算函数调用,最后计算
*

*(my_struct.my_pointer_to_a_function(value)); //Error!

因此,函数将被调用,但调用的结果,
int
,将被取消引用,因此出现错误。

指向函数的指针可以按原样使用,而无需任何取消引用:

my_struct.my_pointer_to_a_function(value)
但如果您坚持要取消引用,则必须以这种方式使用括号:

(*my_struct.my_pointer_to_a_function)(value)
两者完全相同,所以我推荐第一种,更简单

关于您第一次尝试:

my_struct.(*my_pointer_to_a_function)(value); //Error!
这是行不通的,因为必须首先计算parenthersis中的表达式:
*my\u pointer\u to\u a\u function
,但这本身并不意味着什么

还有你的第二个:

*my_struct.my_pointer_to_a_function(value); //Error!
运算符优先级规则首先计算
,然后计算函数调用,最后计算
*

*(my_struct.my_pointer_to_a_function(value)); //Error!
因此,将调用函数,但调用的结果,即
int
,将被取消引用,从而产生错误。

使用以下方法:

#define function mystruct.my_pointer_to_a_function
然后可以调用该函数:

int i = function(value);
使用以下命令:

#define function mystruct.my_pointer_to_a_function
然后可以调用该函数:

int i = function(value);


哦,是的,我在用普通C语言编程。试过编译吗?跑步呢?我这么说是因为最好先看看编译器做了什么。你试过了吗?:-
my_struct.my_pointer_to_a_function(value)这应该有效
我的结构。我的指针指向函数(值)哦,是的,我在用纯C编程。试过编译吗?跑步呢?我这么说是因为最好先看看编译器做了什么。你试过了吗?:-
my_struct.my_pointer_to_a_function(value)这应该有效
我的结构。我的指针指向函数(值)我认为编译器指令
#typedef
不存在!也许你的意思是定义。无论如何,这不是一个好主意…@rodrigo是的,这是一个打字错误,我只是在你投了反对票后发现的@罗德里戈为什么?我发现它以前用过很多次。@李东国,因为它没有任何附加值。相反,它增加了关于
函数
到底是什么的困惑…@glglgl:实际上这是我的否决票:(.我发现解释我(很少)的否决票是有礼貌的。我这样做是因为“(不必要的)宏是邪恶的”!我不认为编译器指令
#typedef
存在!也许你的意思是
#define
。无论如何,这不是一个好主意…@rodrigo是的,这是一个拼写错误,我只是在你投了反对票后才发现它!@rodrigo为什么?我发现它以前用过很多次。@LidongGuo,因为它没有增加任何值。相反,它增加了对
函数的混淆。
真的是…@glglglgl:事实上这是我的反对票:(.我发现解释我(很少)的反对票是有礼貌的。我这样做是因为“(不必要的)宏是邪恶的”!