Source File Organization
Lee Derbenwick
lfd at cbnewsm.att.com
Thu Feb 28 05:25:25 AEST 1991
In article <1991Feb26.045242.23453 at rfengr.com>, rfarris at rfengr.com (Rick Farris) writes:
> I have an enumerated type:
>
> typedef enum { A, B, C, D } CMD;
>
> and a corresponding array of ascii representations :
>
> char ltrs[] = { 'A', 'B', 'C', 'D' };
>
> My problem is: How do I keep the darn things in sync?
This is a bit kludgy, and it doesn't _guarantee_ that they're in
sync (i.e., no protection from typos), but its flexible and
general, and _very_ easy to do. The key is an include file using
macros that are defined differently in different contexts.
letters.h contains:
LETTER(A, 'A')
LETTER(B, 'B')
LETTER(C, 'C')
LETTER(D, 'D')
#undef LETTER
Then, to get your two examples:
#define LETTER(A,B) A
typedef enum {
#include "letters.h"
} CMD;
#define LETTER(A,B) B
char ltrs[] = {
#include "letters.h"
};
This trick can also be useful for creating a number of tables
that are really views of a single relation.
-- Speaking strictly for myself,
-- Lee Derbenwick, AT&T Bell Laboratories, Warren, NJ
-- lfd at cbnewsm.ATT.COM or <wherever>!att!cbnewsm!lfd
More information about the Comp.lang.c
mailing list