home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20289 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.2 KB  |  75 lines

  1. Path: sparky!uunet!charon.amdahl.com!netcomsv!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: initialized extern variables, coded in 1 file, used in many
  5. Message-ID: <926@ulogic.UUCP>
  6. Date: 27 Jan 93 18:53:42 GMT
  7. References: <1993Jan26.015543.23671@netcom.com>
  8. Organization: negligable
  9. Lines: 64
  10.  
  11. In article <1993Jan26.015543.23671@netcom.com> pdh@netcom.com (Phil Howard ) writes:
  12. >I want to define a large set of extern variables and I want to define
  13. >them in just one place (one file) so that I don't have any problems
  14. >with things out of sync as I change them (changes of type for existing
  15. >variables, or adding and deleting variables).
  16. >
  17. >For the above would create variables in a header like:
  18. >
  19. >EXTERN long var1;
  20. >EXTERN int var2;
  21. >
  22. >and do
  23. >
  24. >#define EXTERN extern
  25. >
  26. >in all files but the one where they are being exported from.  In that
  27. >one file I would do
  28. >
  29. >#define EXTERN
  30.  
  31. Rather than this, which requires an extra #define in every file
  32. I prefer to put this in the globals.h include file:
  33.  
  34.     #ifdef __MAIN
  35.     #define EXTERN
  36.     #else
  37.     #define EXTERN extern
  38.     #endif
  39.  
  40. and place a "#define __MAIN" in one of my files prior to including
  41. the globals.h.
  42.  
  43. >and include the header before the first block.  The choice of symbol
  44. >name EXTERN is arbitrary; some other name may be used if consistent.
  45. >
  46. >---------------- NOW HERE IS THE CATCH ----------------
  47. >
  48. >I want to initialize many (its ok to be required to initialize all)
  49. >of these variables.  The problem is that initializers are inconsistent
  50. >with the extern storage class.  But the initializers would have to be
  51. >coded in the same file as the types of the variables are coded, i.e.
  52. >the header.  That means the initializers would be there for those modules
  53. >using the variables with the extern class.
  54.  
  55.     EXTERN int uninitialized;
  56.  
  57.     #ifdef EXTERN
  58.     extern int initialized1;
  59.     extern float initialized2;
  60.     extern int initialzed3[];
  61.     #else
  62.     int initialized1 = 50;
  63.     float initialized2 = 3.14;
  64.     int initialzed3[] = { 2, 4, 6 };
  65.     #endif
  66.  
  67.  
  68.  
  69.         -Richard Hartman
  70.         hartman@ulogic.COM
  71.  
  72. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  73. "Who knows what evil lurks in the hearts of men?"
  74.  
  75.