Xref: utzoo comp.lang.c:36613 comp.software-eng:4946 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!uunet!hsi!stpstn!lerman From: lerman@stpstn.UUCP (Ken Lerman) Newsgroups: comp.lang.c,comp.software-eng Subject: Re: Source File Organization Message-ID: <6488@stpstn.UUCP> Date: 27 Feb 91 19:44:43 GMT References: <1991Feb26.045242.23453@rfengr.com> Reply-To: lerman@stpstn.UUCP (Ken Lerman) Organization: The Stepstone Corporation, Sandy Hook, CT 06482 Lines: 57 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' }; > >used for printing and other various purposes. ............. >My problem is: How do I keep the darn things in sync? > >Suppose I add a new CMD, "Z", is there any way to ensure >that ltrs[] is updated? >The problem is exacerbated by the fact that the CMD enum, >being a typedef, is in a header file that is included in >many places. Since ltrs[] is an instantiated variable, it >*can't* live in the same place. Where should it live? .................. >-- >Rick Farris RF Engineering POB M Del Mar, CA 92014 voice (619) 259-6793 >rfarris@rfengr.com ...!ucsd!serene!rfarris serenity bbs 259-7757 I saw this idea in the gnu C compiler. In file defs.h: /* establish the pairs */ xxx(A,'A'), xxx(B,'B'), xxx(C,'C'), xxx(D,'D'), Then in file use1.c: #define xxx(a,b) a typedef enum { #include "defs.h" } CMD; #undef xxx In file use2.c (or in another place in the same file): #define xxx(a,b) b char ltrs[] = { #include "defs.h" }; #undef xxx I haven't actually tried this, or there may be some typos, but I think the idea is worth posting. Ken