如何定义定义结构指针和下面提到的格式。以及如何在代码中访问.servicefunc?

时间:2018-08-24 10:12:21

标签: c pointers structure

CONST(Student_ServiceCfgType, VAR) student[] =
{
    {&Student_Name,    NAME},
    {&Student_age,  AGE}
};

main_function()
{
   /* get the Service index of the Service Configuration table */
    Student_Service =  0; // takes 0th element from above config table.

    /* Send the service */
    status = (*student[Student_Service].ServiceFunc)();

    /* notify application about service finish */
    status = Student_UsrCallback(student[Student_Service].ServiceType, status);
}

struct
{
}Student_ServiceCfgType;


status = (*student[Student_Service]***.ServiceFunc***)();

问题1)请向我解释一下上面的代码是什么意思?

第二季度)可以解释上述代码的工作原理

1 个答案:

答案 0 :(得分:0)

1。请解释一下上面的代码是什么意思?

  • make是指向函数的指针,其中包含返回ServiceFunc类型且不带参数的函数地址。
  

因此,您的status总体上可能如下所示。

struct student
  

您的功能可能如下所示。

typedef struct
{
  status (*ServiceFunc)(); //declaring function pointer
}studentStruct;
  

然后分配函数指针并使用函数指针调用函数,如下所示。

status func1()
 {
      status ret = 0;
      printf("studenti\n");
      return ret;
 }

这就是您在这里所做的。

studentStruct student[1];
student[0].ServiceFunc = &func1;

status st = (*student[0].ServiceFunc)();
  

您的完整代码可能如下所示。

status = (*student[Student_Service].ServiceFunc)();