-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcallback_demo6.c
More file actions
55 lines (49 loc) · 958 Bytes
/
callback_demo6.c
File metadata and controls
55 lines (49 loc) · 958 Bytes
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
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef void(*CBTest)(void *p);
typedef struct _cbDemo CBDemo;
struct _cbDemo
{
CBTest cb;
void *cbParam;
int k;
};
typedef struct _Data Data;
struct _Data
{
int i;
int j;
};
void Test(void *p)
{
((Data*)p)->i++;
((Data*)p)->j++;
}
void Test2(void *p)
{
((CBDemo*)p)->k = 10;
}
void RegisterCallback(CBDemo *pDemo,CBTest Test,void *p)
{
pDemo->cb = Test;
pDemo->cbParam = p;
}
int main()
{
CBDemo demo;
Data data;
data.i = 10;
data.j = 11;
demo.k = 1;
RegisterCallback(&demo,Test,&data);
printf("RegisterCallback Test: %d,%d\n\r",data.i,data.j);
demo.cb(demo.cbParam);
printf("demo.cb: %d,%d\n\r",data.i,data.j);
RegisterCallback(&demo,Test2,&demo);
printf("RegisterCallback Test2: %d\n",demo.k);
demo.cb(demo.cbParam);
printf("demo.cb: %d\n\r",demo.k);
return 0;
}