Source File Organization

Ken Lerman lerman at stpstn.UUCP
Thu Feb 28 06:44:43 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' };
>
>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 at 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



More information about the Comp.lang.c mailing list