void TestOperation_Init(const void *Ptr)
{
/*do something*/
}
struct FeatureStruct
{
const Select *S;
void (*Init)(const void *Ptr);
};
sturct Cnf
{
const FeatureStruct *Feature;
};
const Cnf Cnf_1[0]=
{
&FeatureStruct_1[0],
};
const FeatureStruct FeatureStruct_1[0]=
{
(Select*)&TestStruct,
TestOperation_Init,
};
const Cnf *CnfPtr;
void Main_Init(const Cnf *MainCnfPtr)
{
CnfPtr=MainCnfPtr;
CnfPtr->Feature->Init(CnfPtr->Feature->S);
}
User will call
Main_Init(&Cnf_1[0]);
Now if I want to access member of feature struct
I write it like this
CnfPtr->Feature->Init(CnfPtr->Feature->S);
But compiler gives error at "Feature->Init" and Feature->S" that left side of -> is not struct
If I write like this
CnfPtr->Feature.Init(CnfPtr->Feature.S);
It works and gives not error.
My question is, What is correct way to access function pointer which is memeber of another structure ? Because Feature is pointer to the structure and it points to "Init" function pointer.
Thank you in advance.
Take a look at the below code snippet which will show how to access function pointers within structure.Please make a note that C is case sensitive. In your code you tend to just ignore it.
Question:
What is correct way to access function pointer which is memeber of another structure ?
struct FeatureStruct
{
void (*Init)();
};
struct Cnf
{
struct FeatureStruct *Feature;
};
void func()
{
printf("Hi\n");
}
int main(void) {
struct Cnf *CnfPtr;
CnfPtr = malloc(sizeof(struct Cnf));
CnfPtr->Feature = malloc(sizeof(struct FeatureStruct));
CnfPtr->Feature->Init = func;/* Initialize your pointer */
CnfPtr->Feature->Init();
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.