home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16803 < prev    next >
Encoding:
Text File  |  1992-11-19  |  1.6 KB  |  62 lines

  1. Path: sparky!uunet!stanford.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Where are literals stored?
  5. Date: 19 Nov 1992 18:42:06 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 49
  8. Message-ID: <27542@dog.ee.lbl.gov>
  9. References: <1992Nov18.233739.2335@den.mmc.com>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <1992Nov18.233739.2335@den.mmc.com>
  14. richard@crowded-house.den.mmc.com (Richard Armstrong) writes:
  15. >Are literals always stored in the same location in memory?
  16.  
  17. This is unspecified.  The C standard allows identical literals to live
  18. at the same address or at differing addresses, as the implementation
  19. pleases.
  20.  
  21. >(IBM-PC,Borland)
  22.  
  23. Please try to limit system-specific questions to system-specific
  24. newsgroups.
  25.  
  26. >For instance, is the string a stored in the same place in the following
  27. >two declarations?:
  28. >
  29. >static char a[]="ABC"
  30. >
  31. >funca()
  32. >{
  33. >char a[]="ABC";
  34. >}
  35.  
  36. Neither of these is in fact a `string literal': in both cases the
  37. quoted strings are initializers for objects.  String literals are
  38. formed from quoted strings that appear in a value context, e.g.,
  39.  
  40.     char *p, *q;
  41.  
  42.     p = "ABC";
  43.     q = "ABC";
  44.     /* it is unspecified whether p == q */
  45.  
  46. Distinct objects, however, always compare unequal, thus the following
  47. strictly conformant program must print `0':
  48.  
  49.     #include <stdio.h>
  50.     #include <stdlib.h>
  51.  
  52.     char a[] = "ABC";
  53.     int main(void) {
  54.         char b[] = "ABC";
  55.  
  56.         printf("%d\n", a == b);
  57.         exit(EXIT_SUCCESS);
  58.     }
  59. -- 
  60. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  61. Berkeley, CA        Domain:    torek@ee.lbl.gov
  62.