X-Marco

 

http://www.drdobbs.com/cpp/184401387

 

//------------------ File: color_table.h
X(red, "red")
X(green, "green")
X(blue, "blue")

// ----------------- File: main.c
#include <stdio.h>

#define X(a, b) a,
enum COLOR {
#include "color_table.h"
};
#undef X

#define X(a, b) b,
char *color_name[] = {
#include "color_table.h"
};
#undef X

int main() {
enum COLOR c = red;
printf("c=%s\n", color_name[c]);
return 0;
}

 

Note: enum and char *[] are combined in a single include file color_table.h

          espacilly, C has designed initializer attribute

 

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fdesignators.htm

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 阿肯 的頭像
    阿肯

    韌體開發筆記

    阿肯 發表在 痞客邦 留言(1) 人氣()