【问题标题】:What does the following C code do, = { }下面的 C 代码做了什么,= { }
【发布时间】:2016-02-14 14:07:36
【问题描述】:

下面的代码包含在 STMicroelectronics C 代码中,用于 USB 驱动程序。 我试图了解(理解)它是如何工作的,我承认我的 C 编程并不强。

我的问题是 / 是什么

USBD_Interface_fops_FS =
{ xxxxx }

???

我在 K & R 中找不到,在类似的示例中使用了“=”赋值运算符。

typedef struct _USBD_CDC_Itf
{
  int8_t (* Init)          (void);
  int8_t (* DeInit)        (void);
  int8_t (* Control)       (uint8_t, uint8_t * , uint16_t);   
  int8_t (* Receive)       (uint8_t *, uint32_t *);  

}USBD_CDC_ItfTypeDef;

static int8_t CDC_Init_FS     (void);
static int8_t CDC_DeInit_FS   (void);
static int8_t CDC_Control_FS  (uint8_t cmd, uint8_t* pbuf, uint16_t length);
static int8_t CDC_Receive_FS  (uint8_t* pbuf, uint32_t *Len);

USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = 
{
  CDC_Init_FS,
  CDC_DeInit_FS,
  CDC_Control_FS,  
  CDC_Receive_FS
};

【问题讨论】:

  • 使用 K&R 作为准现代 C 编程的参考,就像尝试使用中古英语词典理解英语一样。你会弄错很多小事,并错过自发布以来的所有更改。

标签: c struct typedef assignment-operator


【解决方案1】:

它是struct initializer syntax。它只是将结构中的四个函数指针设置为四个命名函数。

【讨论】:

    【解决方案2】:

    结构初始化。

    这个:

    USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = 
    {
      CDC_Init_FS,
      CDC_DeInit_FS,
      CDC_Control_FS,  
      CDC_Receive_FS
    };
    

    只是一种更有效的方法:

    USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
    USBD_Interface_fops_FS.Init = CDC_Init_FS;
    USBD_Interface_fops_FS.DeInit = CDC_DeInit_FS;
    USBD_Interface_fops_FS.Control = CDC_Control_FS;
    USBD_Interface_fops_FS.Receive = CDC_Receive_FS;
    

    我确定这从 ANSI C 89 开始就已经存在。我不知道他们是否在 K & R 中进行了结构初始化...

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多