简体   繁体   English

在struct中使用函数指针-C编译器

[英]Use function pointer in struct - C compiler

I found an answer how to make function pointer in struct but I am still curious about its operation. 我找到了如何在struct中创建函数指针的答案,但我仍然对其操作感到好奇。 Can anybody explain this clearly? 有人可以清楚地解释吗?

Here is my question, 这是我的问题

This code runs properly but... 这段代码可以正常运行,但是...

char func_1(void);    
char func_2(char);

struct mStruct
{
    char name[100]; 
    int age;
    char (*func_1)(void);
    void (*func_2)(char);
};

void init_struct(struct mStruct *pStruct)<-why this void function is necessary? 
{
    if(pStruct == NULL) {
        pStruct = malloc(sizeof(struct mStruct));
    }

    (*pStruct).age = 25;
    (*pStruct).func1 = &func1;
    (*pStruct).func2 = &func2;
 }

 char func_1(void)
 {
   ... ;
  }    
 char func_2(char)
 {
   ... ;
  }

I already tried to eliminate the init_struct function, but all tries failed. 我已经尝试消除init_struct函数,但是所有尝试都失败了。 My gcc compiler only accepts it as above. 我的gcc编译器仅接受上述内容。 Does anybody know another way to initialize the struct without using a function or why it is only acceptable as a void function? 有人知道不使用函数来初始化结构的另一种方法吗,或者为什么它只能作为void函数被接受?

Actually, besides the title, your question seems to have nothing to do with function pointers. 实际上,除了标题之外,您的问题似乎与函数指针无关。

You have a struct mStruct with some members. 您有一个带有某些成员的结构mStruct For your question, it does not really matter what these members are. 对于您的问题,这些成员的身份并不重要。

With this struct, you don't need an initialization function. 使用此结构,您不需要初始化函数。 You can always simply use the struct and initialize it: 您总是可以简单地使用该结构并将其初始化:

struct mStruct s;
s.member = 20;

But, this comes with a cost: You repeat yourself and when you want to change the struct (say, add another member), you will have to change lot of places. 但是,这需要付出一定的代价:您重复自己,并且要更改结构时(例如,添加另一个成员),则必须更改很多位置。 That's bad and can be fixed with an initialization function: 不好,可以用初始化函数解决:

void init_struct(struct mStruct *pStruct)
{
    pStruct->member = 20;
}

struct mStruct s;
init_struct(&s);

Now, you should not add other responsibilities to init_struct than to initialize it. 现在,除了初始化外,您不应该向init_struct添加其他职责。 In your example, you also allocate memory for the struct, in a buggy way. 在您的示例中,您还以错误的方式为该结构分配了内存。 Instead, use another function for that: 而是使用另一个函数:

struct mStruct* create_struct()
{
    struct mStruct *pStruct = (struct mStruct*) malloc(sizeof(struct mStruct));
    init_struct(pStruct);
    return pStruct;
}

struct mStruct *pStruct = create_struct();
do_something_with(pStruct);
free(pStruct); // Don't forget this!

If you need more cleanup than just freeing the memory, write yet another function for it: 如果您需要的不仅仅是清理内存,还需要编写另一个函数:

void destroy_struct(struct mStruct *pStruct)
{
    cleanup(pStruct);
    free(pStruct);
}

struct mStruct *pStruct = create_struct();
do_something_with(pStruct);
destroy_struct(pStruct); // Don't forget this!

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

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