Xref: utzoo comp.lang.c:36586 comp.software-eng:4939 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!linac!att!att!cbnewsm!lfd From: lfd@cbnewsm.att.com (Lee Derbenwick) Newsgroups: comp.lang.c,comp.software-eng Subject: Re: Source File Organization Summary: a trick using macros and another include file Message-ID: <1991Feb27.182525.29758@cbnewsm.att.com> Date: 27 Feb 91 18:25:25 GMT References: <1991Feb26.045242.23453@rfengr.com> Followup-To: comp.lang.c Organization: AT&T Bell Laboratories Lines: 42 In article <1991Feb26.045242.23453@rfengr.com>, rfarris@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@cbnewsm.ATT.COM or !att!cbnewsm!lfd