【问题标题】:Function pointer in struct, taking the struct as argument in Cstruct中的函数指针,将struct作为C中的参数
【发布时间】:2020-07-17 15:57:22
【问题描述】:

如何在结构中使用函数指针,使用结构作为参数?

我有这个:

typedef struct button {
    char SizeUnit;
    char Status;
    
    int Width;
    int Height;
    
    
    char *Text;
    Color TextColor;
    Color BGColor;
    Color BorderColor;
    int BorderWidth;
    
    
    void (*ActionL)(button *bt);
    void (*ActionR)(button *bt);
    void (*ActionM)(button *bt);
    
    void (*Hover) (button *bt);
    void (*draw) (button *bt);
} button;

我怎样才能让它工作?

【问题讨论】:

  • 我应该认为你只是写struct button *?因为在遇到函数指针定义的时候,还没有遇到typedef。 (或者更确切地说,正在处理中。)
  • 要么使用@Peter 上面提到的struct button *,要么先声明typedef,如下所示:typedef struct button button;struct button { ... };
  • 或者,停止对你的结构进行类型定义。 Typedef 非常适合函数类型和不透明类型,但坦率地说,它们在应用于结构时只是混淆。
  • 您尝试伪造 C++ 风格的成员函数将导致意外的接口,因为以下是有效的调用:buttonA->draw(buttonB); 尽管在对象 buttonA 上被调用,但它确实会绘制 buttonB。一个明智的电话buttonA->draw(buttonA); 违反了不要重复自己的规则。

标签: c struct typedef


【解决方案1】:

typedef(被识别的符号)的“效果”在结构本身中可用

你有两个选择:

  1. 在定义函数指针时只需参考struct button

     void (*ActionL)(struct button *bt);`
    
  2. 在结构定义之前写typedef(前向声明):

     typedef struct button_s button_t;
    
     typedef  struct button_s {
         char SizeUnit;
         char Status;
    
         int Width;
         int Height;
    
         char *Text;
         Color TextColor;
         Color BGColor;
         Color BorderColor;
         int BorderWidth;
    
         void (*ActionL)(button_t *bt);
         void (*ActionR)(button_t *bt);
         void (*ActionM)(button_t *bt);
    
         void (*Hover) (button_t *bt);
         void (*draw) (button_t *bt);
     } button_t;
    

    在这里,我在 typedef_t 之前为结构体使用了一个尾随 _s 的约定,用于新定义的类型。

【讨论】:

    【解决方案2】:

    除了 Roberto 的回答,我想更深入地讨论他的第 1 点。这是一回事,structs 比 typedefs 更有益。

    当你只使用结构标记作为指针的引用时,你可以省略特定结构标记的前向声明。该结构是不完整的,并在其后面的定义中完成。

    相关:

    当您改用typedef 时,需要先定义别名。这是一个缺点。

    因此,这是完全有效的 (Proof):

    请注意,如果您愿意,可以在此处省略 typedefs,因为它们至少在这段代码中是多余的。但是如果使用 ,它们不会与不完整的结构声明冲突,因为它们都位于不同的命名空间中,您需要使用 struct 关键字来符号化结构(标签)。

    #include <stdio.h>
    
    typedef struct Color {
        unsigned int red;
        unsigned int green;
        unsigned int blue;
    } Color;
    
    
    struct button {
        char SizeUnit;
        char Status;
        
        int Width;
        int Height;
        
        
        char *Text;
        struct Color TextColor;
        struct Color BGColor;
        struct Color BorderColor;
        int BorderWidth;
        
        
        void (*ActionL)(struct button *bt);
        void (*ActionR)(struct button *bt);
        void (*ActionM)(struct button *bt);
        
        void (*Hover) (struct button *bt);
        void (*draw) (struct button *bt);
    } button;
    

    我个人倾向于根本不使用typedefs。这受制于:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多