home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12237 < prev    next >
Encoding:
Internet Message Format  |  1992-08-12  |  2.0 KB

  1. From: richw@inmet.camb.inmet.com
  2. Date: 12 Aug 1992 10:48 EDT
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Computer terms, etc
  5. Message-ID: <20900024@inmet>
  6. Path: sparky!uunet!inmet!inmet!richw
  7. Nf-ID: #R:<cee1:713424953:inmet:20900024:000:1740
  8. Nf-From: inmet.camb.inmet.com!richw    Aug 12 10:48:00 1992
  9. References: <713424953@<cee1>
  10. Lines: 63
  11.  
  12.  
  13. >>  char *foo = "abc";
  14. >>  char *foo[] = "abc";
  15. >>  char foo[] = "abc";
  16. >>  char foo[4] = "abc";
  17.  
  18. First, let me give these "foo"s unique names:
  19.  
  20.     char *fooA = "abc";
  21.     char *fooB[] = "abc";
  22.     char fooC[] = "abc";
  23.     char fooD[4] = "abc";
  24.  
  25. Here's a way of rewriting these which might make things clearer:
  26.  
  27.     static char literal [] = { 'a', 'b', 'c', '\0' };
  28.  
  29.     /*  "literal" is now a constant which equals the address  */
  30.     /*  of the first element of an initialized, 4-element     */
  31.     /*  "char" array                          */
  32.  
  33.     char *fooA = literal;
  34.     char *fooB[] = (char **) literal;
  35.     #define fooC literal
  36.     #define fooD literal
  37.  
  38. Thus, "fooC" and "fooD" are identical; both are constants which equal the
  39. address of the "literal" array's first element.
  40.  
  41. "fooA", on the other hand, is an initialized variable which can be re-
  42. assigned, i.e. the following are legal:
  43.  
  44.     char *ptr;
  45.  
  46.     fooA = "some other literal";
  47.     fooA = ptr;
  48.  
  49. but this isn't:
  50.  
  51.     fooC = "some other literal";
  52.  
  53. Note that the "4" in the "char fooD[4]" declaration is entirely unnecessary;
  54. what's interesting are these cases:
  55.  
  56.     fooD[3] = "abc";    /*  Illegal !  "fooD" is too small  */
  57.  
  58.     fooD[5] = "abc";    /*  "fooD" is the constant address  */
  59.                 /*  of the first element of a 5-    */
  60.                 /*  element "char" array whose 5-th */
  61.                 /*  element is uninitialized        */
  62.  
  63. Finally, note that "char *fooB[]" represents a pointer to an array, where
  64. each element of the array is itself a pointer to "char".  This is a
  65. *different* type than the type of "abc", i.e. a pointer to "char".  I'm
  66. surprised my C compiler accepted for "fooB" declaration.  In any case,
  67. I would never write such a declaration...
  68.  
  69.  
  70. Rich Wagner
  71.     Intermetrics, Inc.
  72.     richw@inmet.camb.inmet.com
  73.  
  74.  
  75.