如何使用struct作为c中的参数来调用函数

时间:2016-01-22 22:03:40

标签: c struct

我有一个处理触摸显示器事件的功能。事件包含在struct

struct genieReplyStruct
{
  int cmd ;
  int object ;
  int index ;
  unsigned int data ;
} ;

void handleGenieEvent (struct genieReplyStruct *reply)
{
  if (reply->cmd != GENIE_REPORT_EVENT)
  {
    printf ("Invalid event from the display: 0x%02X\r\n", reply->cmd) ;
    return ;
  }

  /**/ if (reply->object == GENIE_OBJ_KEYBOARD)
  {
    if (reply->index == 0)  // Only one keyboard
      calculatorKey (reply->data) ;
    else
      printf ("Unknown keyboard: %d\n", reply->index) ;
  }
  else if (reply->object == GENIE_OBJ_WINBUTTON)
  {
    /**/ if (reply->index == 1) // Clock button on main display
      genieWriteObj (GENIE_OBJ_FORM, 1, 0) ;
    else if (reply->index == 0) // Calculator button on clock display
    {
      genieWriteObj (GENIE_OBJ_FORM, 0, 0) ;
      updateDisplay () ;
    }
    else
      printf ("Unknown button: %d\n", reply->index) ;
  }
  else
    printf ("Unhandled Event: object: %2d, index: %d data: %d [%02X %02X %04X]\r\n",
      reply->object, reply->index, reply->data, reply->object, reply->index, reply->data) ;
}

现在我想模拟调用此handleGenieEvent函数,但我不确定如何:我尝试将struct打印为int,然后调用它但不起作用我也尝试用数组调用它:

 void handleGenieEvent (-1090701828);
void handleGenieEvent ([21 0A 0000]); 

2 个答案:

答案 0 :(得分:3)

使用C99你可以使用复合文字:

handleGenieEvent(&(struct genieReplyStruct){GENIE_REPORT_EVENT, 0, 0, 0}); 

答案 1 :(得分:3)

使用C99或更高版本,使用复合文字,详见C11 6.5.2.5

handleGenieEvent(&(struct genieReplyStruct) { 0,1,2,3});

或模拟每个OP的[21 0A 0000]

handleGenieEvent(&(struct genieReplyStruct) { cmd, 0x21, 0x0A, 0x0000});