1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct Frame {
void (*send_data)();
} Frame_s;
typedef struct GenericFrame {
Frame_s frame;
} GenericFrame_s;
typedef struct FrameA {
Frame_s frame;
int n;
} FrameA_s;
typedef struct FrameB {
Frame_s frame;
char str[20];
} FrameB_s;
typedef struct FrameC {
Frame_s frame;
double f;
} FrameC_s;
void sendDataFrameA(FrameA_s * frame) {
printf("I send the frame A with n=%d\n", frame->n);
}
void sendDataFrameB(FrameB_s * frame) {
printf("I send the frame B with string:%s\n", frame->str);
}
void sendDataFrameC(FrameC_s * frame) {
printf("I send the frame C with f=%lf\n", frame->f);
}
int main(void)
{
GenericFrame_s * frames[3];
FrameA_s * fA = NULL;
FrameB_s * fB = NULL;
FrameC_s * fC = NULL;
int i;
/* TODO : Verifier allocations*/
fA = malloc(sizeof(*fA));
fB = malloc(sizeof(*fA));
fC = malloc(sizeof(*fA));
fA->frame.send_data = sendDataFrameA;
fA->n = 51;
fB->frame.send_data = sendDataFrameB;
strcpy(fB->str, "hello");
fC->frame.send_data = sendDataFrameC;
fC->f = 13.2;
frames[0] = (GenericFrame_s*)fA;
frames[1] = (GenericFrame_s*)fB;
frames[2] = (GenericFrame_s*)fC;
for(i=0; i<3; ++i) {
frames[i]->frame.send_data(frames[i]);
}
free(fA);
free(fB);
free(fC);
return 0;
} |