-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathequivalent_enum_match.c
68 lines (58 loc) · 1.38 KB
/
equivalent_enum_match.c
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
typedef struct {
char *name;
void *payload;
} image_t;
typedef struct {
char *name;
char *mail;
uint32_t age;
image_t img;
char *bio;
} user_t;
typedef enum {MESSAGE_CONTENT, IMAGE, SHARED_CONTACT} message_e;
typedef struct {
message_e type;
union {
char *message_content;
image_t image;
user_t shared_contact;
} payload;
} message_t;
image_t create_image(char *name, void *payload, size_t payload_size)
{
return (image_t){strdup(name), strndup(payload, payload_size)};
}
message_t *fetch_message()
{
message_t *msg = malloc(sizeof(*msg));
if (!msg) {
return NULL;
}
*msg = (message_t){MESSAGE_CONTENT, {.message_content = strdup("Damn")}};
return msg;
}
int main(int argc, char *argv[])
{
message_t *msg = fetch_message();
if (!msg) {
return 0;
}
switch (msg->type) {
case MESSAGE_CONTENT:
printf("Received new message -> %s\n", msg->payload.message_content);
free(msg->payload.message_content);
break;
case IMAGE:
printf("There is an image\n");
break;
case SHARED_CONTACT:
printf("Shared contact name -> %s\n", msg->payload.shared_contact.name);
break;
}
free(msg);
}