home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3020 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  1.4 KB

  1. Path: sparky!uunet!sdrc!thor!scjones
  2. From: scjones@thor.sdrc.com (Larry Jones)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Casting const; Is this standard conforming?
  5. Message-ID: <2268@sdrc.COM>
  6. Date: 17 Nov 92 14:44:02 GMT
  7. References: <1992Nov16.200843.5565@Princeton.EDU>
  8. Sender: news@sdrc.COM
  9. Lines: 26
  10.  
  11. In article <1992Nov16.200843.5565@Princeton.EDU>, rhl@grendel.Princeton.EDU (Robert Lupton the Good) writes:
  12. >    const int i = 0;
  13. >    
  14. >    (int)i = 1;                /* Fails */
  15.  
  16. The result of a cast is not an l-value and thus cannot be assigned to.
  17.  
  18. >    *(int *)&i = 1;            /* OK */
  19.  
  20. This is acceptable syntax, but results in undefined behavior since i is
  21. defined as a const variable.  The compiler is allowed to place it in
  22. read-only memory, which would cause the assignment to fail.
  23.  
  24. > I want to set
  25. > the variable once in a private place just after mallocing it, in case
  26. > you wonder why I am trying to get around a const restriction, and
  27. > don't see why I can't just initialise it
  28.  
  29. That's a different case than your example: the memory that malloc()
  30. returns is *not* defined with the const qualifier, just the pointer that
  31. you are using to access it.  In that case the second method is well
  32. defined and portable.
  33. ----
  34. Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
  35. larry.jones@sdrc.com  or  ...uunet!sdrc!larry.jones
  36. OK, what's the NEXT amendment say?  I know it's in here someplace. -- Calvin
  37.