如何定义定义结构指针和下面提到的格式。以及如何在代码中访问 .servicefunc?
how to define define structure pointer and below mentioned format. and how to access .servicefunc in code?
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)请解释一下上面这行代码是什么意思?
Q2) 可以解释上面的代码是如何工作的
1.请解释一下上面这行代码是什么意思?
ServiceFunc
是指向函数的指针,它保存 returns status
类型的函数的地址并且不带参数。
So overall your struct student
might look like below.
typedef struct
{
status (*ServiceFunc)(); //declaring function pointer
}studentStruct;
And you function might look like below.
status func1()
{
status ret = 0;
printf("studenti\n");
return ret;
}
And you assign the function pointer and call the function using function pointer as below.
studentStruct student[1];
student[0].ServiceFunc = &func1;
status st = (*student[0].ServiceFunc)();
这正是您在这里所做的。
status = (*student[Student_Service].ServiceFunc)();
Your complete code might look like below.
#include<stdio.h>
typedef int status;
typedef struct
{
status (*ServiceFunc)();
}studentStruct;
status func1()
{
printf("studenti\n");
return 0;
}
int main()
{
studentStruct student[10];
student[0].ServiceFunc = &func1;
status st = (*student[0].ServiceFunc)();
return 0;
}
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)请解释一下上面这行代码是什么意思?
Q2) 可以解释上面的代码是如何工作的
1.请解释一下上面这行代码是什么意思?
ServiceFunc
是指向函数的指针,它保存 returnsstatus
类型的函数的地址并且不带参数。
So overall your
struct student
might look like below.
typedef struct
{
status (*ServiceFunc)(); //declaring function pointer
}studentStruct;
And you function might look like below.
status func1()
{
status ret = 0;
printf("studenti\n");
return ret;
}
And you assign the function pointer and call the function using function pointer as below.
studentStruct student[1];
student[0].ServiceFunc = &func1;
status st = (*student[0].ServiceFunc)();
这正是您在这里所做的。
status = (*student[Student_Service].ServiceFunc)();
Your complete code might look like below.
#include<stdio.h>
typedef int status;
typedef struct
{
status (*ServiceFunc)();
}studentStruct;
status func1()
{
printf("studenti\n");
return 0;
}
int main()
{
studentStruct student[10];
student[0].ServiceFunc = &func1;
status st = (*student[0].ServiceFunc)();
return 0;
}