-
Notifications
You must be signed in to change notification settings - Fork 3
/
datadrive.c
38 lines (31 loc) · 886 Bytes
/
datadrive.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
/* datadrive.c -- demonstrate that separating code and data can result in tighter output */
#include <string.h>
int nameToNumber(const char *name)
{
if (strcmp(name, "John") == 0) return 5;
else if (strcmp(name, "Paul") == 0) return 2;
else if (strcmp(name, "George") == 0) return 9;
else if (strcmp(name, "Ringo") == 0) return 3;
else return -1; /* default case */
}
#define NELEMS(a) ((int) (sizeof(a) / sizeof(a[0])))
int nameToNumber2(const char *name)
{
static const struct
{
const char name[7]; /* NB. PIC */
int value;
}
map[] =
{
{ "John", 5 },
{ "Paul", 2 },
{ "George", 9 },
{ "Ringo", 3 }
};
int i;
for (i = 0; i < NELEMS(map); i++)
if (strcmp(name, map[i].name) == 0)
return map[i].value;
return -1; /* default case */
}