API.h
#ifndef API
#define API
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
int face;
float height;
short weight;
} StructTypeA;
typedef struct
{
int sex;
string background;
} StructTypeB;
typedef union
{
StructTypeA appearance;
StructTypeB attribute;
}UnionType;
typedef void(*pEvaluate) (UnionType *unionA);
typedef void(*pFunctionB) (int factor);
typedef struct
{
pEvaluate pA;
pFunctionB pB;
}StructforpFunction;
void Register(StructforpFunction* pStructC1);
void CallBack(void);
#endif
main.c
#include "api.h"
void Evaluate(UnionType *unionA);
int main()
{
StructforpFunction functionstruct =
{
Evaluate
};
Register(&functionstruct);
CallBack();
}
void Evaluate(UnionType *unionA)
{
switch (unionA->attribute.sex)
{
case 1:
printf("a girl!");
cout<< unionA->attribute.background;
while (1);
break;
case 2:
printf("a boy...");
break;
default:
break;
}
};
API.c
#include "api.h"
static StructforpFunction *api;
void Register(StructforpFunction* pStructC1)
{
if (pStructC1)
{
api = pStructC1;
}
}
StructTypeB B = { 1 , "my girlfriend" };
StructTypeB *pB = &B;
void CallBack(void)
{
api->pA((UnionType *)pB);
}