home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19602 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  3.0 KB

  1. Path: sparky!uunet!spool.mu.edu!uwm.edu!ogicse!flop.ENGR.ORST.EDU!gaia.ucs.orst.edu!umn.edu!noc.msc.net!news.stolaf.edu!guenther
  2. From: guenther@stolaf.edu (Philip Guenther)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: const and pointers in ANSI C
  5. Message-ID: <GUENTHER.93Jan12150207@snookles.stolaf.edu>
  6. Date: 12 Jan 93 21:02:27 GMT
  7. Article-I.D.: snookles.GUENTHER.93Jan12150207
  8. References: <1993Jan12.164949.15684@clsi.COM>
  9. Sender: news@news.stolaf.edu
  10. Organization: Academic Computing Center, St. Olaf College
  11. Lines: 65
  12. In-Reply-To: shankha@clsi.COM's message of Tue, 12 Jan 93 16:49:49 GMT
  13.  
  14. In article <1993Jan12.164949.15684@clsi.COM> shankha@clsi.COM (Shankha Mitra) writes:
  15.    Consider the following C code:
  16.        int * const constant_pointer;
  17.        const int *pointer_to_constant_data;
  18.    So the location of the type qualifier const in the declaration
  19.    determines whether the pointer itself or the contents is the
  20.    constant entity. 
  21.  
  22.    Now suppose you use this instead:
  23.  
  24.        typedef int *int_pointer;
  25.        const int_pointer x;
  26.        int_pointer const y;
  27.  
  28.    My compiler (xlc on the RS/6000) which is fully ANSI compatible (or
  29.    so they claim) considers the declarations of x and y to be
  30.    equivalent; it considers x and y to be constant pointers.  That is
  31.    I can say, 
  32.  
  33.        *x = *y = 1;
  34.  
  35.    but not
  36.  
  37.        x = y = &i;
  38.  
  39.    Interesting, huh?
  40.  
  41. How about the differences between the following:
  42.     const int i;
  43.     int const i;
  44. There are none!  Your problem is that you see a typedef as a pretty
  45. macro. It isn't.  When you say 'typedef int *int_pointer', you have
  46. created a new type.  (As a gross over simplification) when you declare
  47. a variable of a given type, you reserve a memory area big enough to
  48. hold that type *without interpreting what the type 'means'*.  Thus the
  49. fact that you declared y to be "const int_pointer" says that y must be
  50. large enough to hold an "int_pointer" (whatever that is!) and that it
  51. can't be modified.
  52.  
  53.    My questions: 
  54.        1) Why this disparity in behavior between using explicit
  55.           types (the first code fragment) and typedefed types
  56.          (second code fragment)? 
  57.  
  58. In the one, the const is either part of the type or a type modifier
  59. while in the other (the typedef case), the const is only a type
  60. modifier.
  61.  
  62.        2) Is it possible to make a declaration of a pointer to
  63.           constant data using the typdefed int_pointer?  If so, how?
  64.  
  65. No.  This would require the compiler to take apart the typedefed type
  66. to determine how to apply the const.  This isn't hard, but it isn't
  67. desireable.  You can already do it with macros and it would break the
  68. concept of the typedef.  Consider what would happen if you later had
  69. to change int_pointer to be a struct or a pointer to a pointer, or...
  70.  
  71.    Shankha
  72.  
  73. Philip Guenther
  74. --
  75. guenther@stolaf.edu (Philip Guenther)       | The ACC might agree with me,
  76. Student Sys Prog, Academic Computing Center | but with that bunch, (and me)
  77. St Olaf College, Northfield, MN 55057       | you never know... :-| :-( :-)
  78. "Life makes sense?  LIFE MAKES SENSE!?!?  Where do people get these ideas?"
  79.