简体   繁体   English

指针指向 function 的结构

[英]Struct with pointer to function

can you please explain in details this line of code inside struct: There is a pointer to function but why would you reference it to struct?您能否详细解释结构中的这一行代码:有一个指向 function 的指针,但为什么要将它引用到结构中?

void (*function)(struct Structure *); 

what does this mean这是什么意思

(struct Structure *)? 
(struct Structure *)

It means that the function have a struct Structure * argument.这意味着 function 有一个struct Structure *参数。 Actually it will make more sense with (struct Structure *variable of struct) .实际上,使用(struct Structure *variable of struct)会更有意义。 In this way, you can use a pointer to point a struct and should put the address of the struct variable which can be used in the function.这样就可以用一个指针指向一个struct,应该把struct变量的地址放到function中。

#include <stdio.h>

typedef struct circle{
    int rad;
    int area;
} Circle;

void ShowCircleInfo(Circle *info)
{
    printf("rad value: %d\n", info->rad);
    printf("area value: %d", info->area);
}

int main(void)
{
    Circle circle_one;
    circle_one.rad = 2;
    circle_one.area = 3;
    ShowCircleInfo(&circle_one);
    
    return 0;
}   

void (*function)(struct Structure *); declares function to be a pointer to a function that has a parameter of type struct Structure * and does not return a value.function声明为指向 function 的指针,该指针具有struct Structure *类型的参数并且不返回值。

For example例如

#include <stdio.h>

struct Structure {
    int a;
    void (*function)(struct Structure *);
};

void foo(struct Structure *a) {
    if (a->function == NULL) a->function = foo;
    a->a++;
    printf("%d\n", a->a);
}

int main(void) {
    struct Structure a = {42, foo};
    struct Structure b = {0}; // don't call b.function just yet!!
    a.function(&b); // foo(&b)
    b.function(&a); // foo(&a)
}

See code running at https://ideone.com/7E74gb查看运行在https://ideone.com/7E74gb的代码

In C, function pointer declarations have almost the same structure as function headers.在C、function指针声明中,与function头的结构几乎相同。 Only the function name will change to have some parantheses and a "*" in it, and the arguments won't have names, because only their types are important when using pointers (we don't access the values of the arguments, so we don't need their names).只有 function 名称会更改为包含一些括号和“*”,而 arguments 不会有名称,因为在使用指针时只有它们的类型很重要(我们不访问 arguments 的值,所以我们不需要他们的名字)。

They basically look like this:它们基本上是这样的:

<return_value> (*<function_name>)(<argument_list>)

So, for example, the function pointer for the function因此,例如 function 指针为 function

void swap(int* a, int* b);

would be将是

void (*swap_ptr)(int*, int*);

Notice that the name of the pointer is in the place of the name of the function, and looks a bit odd compared to normal pointer declarations.请注意,指针的名称位于 function 的名称位置,与普通指针声明相比看起来有点奇怪。

An excellent reading on this topic (you can skip the C++ stuff): https://www.cprogramming.com/tutorial/function-pointers.html关于这个主题的优秀读物(你可以跳过 C++ 的东西): https://www.cprogramming.com/tutorial/function-pointers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM