Path: utzoo!utgpu!cunews!bnrgate!bigsur!bnr-rsc!bcarh181!schow From: schow@bcarh181.bnr.ca (Stanley T.H. Chow) Newsgroups: comp.software-eng Subject: Re: Source File Organization Message-ID: <4070@bnr-rsc.UUCP> Date: 27 Feb 91 22:51:39 GMT References: <1991Feb26.045242.23453@rfengr.com> <4836@goanna.cs.rmit.oz.au> Sender: news@bnr-rsc.UUCP Reply-To: bcarh185!schow@bnr-rsc.UUCP (Stanley T.H. Chow) Organization: BNR Ottawa, Canada Lines: 83 Summary: Followup-To: Keywords: [Since I don't want generate megaflames, the distribution has been trimmed. In particular, comp.lang.c] In article <1991Feb26.045242.23453@rfengr.com>, rfarris@rfengr.com (Rick Farris) writes: > I have a problem that I'm sure has been solved in the C > language before; would someone point me in the right > direction? > > typedef enum { A, B, C, D } CMD; > char ltrs[] = { 'A', 'B', 'C', 'D' }; > > My problem is: How do I keep the darn things in sync? This is a shortcoming of C. In many other languages, especially the strongly typed languages, arrays have specific index type. In your example, "ltrs" would have index type "CMD". If the type and the array got out of sync, the compiler would complaind. Like all other problems of C, for every specific instance of any problem, you can come up with hacks to solve it. Usually, the hacks are non-extensible, and often, it involves considerable work. Since you say your problem is more complicated than your example, I suspect see exactly your problem and which suggestion you adpot.ll be interested to In article <4836@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes: > >I've seen this one often enough that I think it belongs in the FAQ list. >The answer is that you *don't* do it by any special magic in the C source >code itself, but use some other tool to transform a "mini language" to >both files. For example, write a little file like [example with awk script deleted] >(This is not a UNIX-specific solution: make and awk lookalikes are >available for other systems. If you haven't got them, it's trivial >to do this in C itself.) Certainly this example solves this particular problem. What if you want a foo.h that has many different enum ranges? Each range with an array with different structure? The response, of course, is "no problem, here is this other awk script". Also, you have just added extra complexity to the system - some .h files cannot be edited directly and must be regenerated after editing the "real" source. Each particular header file is also likely to have different syntax for its "mini language", making the system harder to understand. Unless you awk script is very clever, the resultent .h files will be hard for humans to read. In Article <17268@milton.u.washington.edu>, black@blake.u.washington.edu (Jum Black) writes: > >One was is to use the C preprocessor, as follows: > >1. Use a macro in an include file that groups all the related items together. >Eg, in "CmdStuff.h": >/* CMD ltr etc */ >CmdStuff (A, 'A', ..) >CmdStuff (B, 'B', ..) >CmdStuff (C, 'C', ..) >CmdStuff (D, 'D', ..) >CmdStuff (Z, 'Z', ..) > >2. Then in each source file (or header, as appropriate), define the CmdStuff >macro to do what you want in the particular case, and #include "CmdStuff.h" >locally. [details deleted] Again, this clearly works. The price is that "CmdStuff.h" file has now totally of "ltrs"? Also, generalizing this to more complicated structures is likely to make the macro a lot more complicated. >P.S. I can't remember if some C compilers might complain about a trailing >comma in the aggregate initialization (mine doesn't) - but you can add a >trailing null/nil/bottom entry in such cases. Often that's useful anyway. Yet another artificial hack to conform to C. Stanley Chow BitNet: schow@BNR.CA BNR UUCP: ..!uunet!bnrgate!bcarh185!schow (613) 763-2831 ..!psuvax1!BNR.CA.bitnet!schow Me? Represent other people? Don't make them laugh so hard.