home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19591 < prev    next >
Encoding:
Text File  |  1993-01-12  |  1.6 KB  |  57 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!clsi!shankha
  3. From: shankha@clsi.COM (Shankha Mitra)
  4. Subject: const and pointers in ANSI C
  5. Message-ID: <1993Jan12.164949.15684@clsi.COM>
  6. Originator: shankha@clsi
  7. Sender: usenet@clsi.COM
  8. Organization: CAD Language Systems, Inc.
  9. Date: Tue, 12 Jan 93 16:49:49 GMT
  10. Lines: 45
  11.  
  12.  
  13.  
  14. Consider the following C code:
  15.  
  16.     int * const constant_pointer;
  17.     const int *pointer_to_constant_data;
  18.  
  19. So the location of the type qualifier const in the declaration determines whether
  20. the pointer itself or the contents is the 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 so they claim)
  29. considers the declarations of x and y to be equivalent; it considers x and y to
  30. be constant pointers.  That is I can say,
  31.  
  32.     *x = *y = 1;
  33.  
  34. but not
  35.  
  36.     x = y = &i;
  37.  
  38. Interesting, huh?
  39.  
  40. The "C Reference Manual" (Harbison and Steele) corroborates the above behavior.
  41. So does gcc -ansi.
  42.  
  43. My questions: 
  44.     1) Why this disparity in behavior between using explicit types (the first
  45.        code fragment) and typedefed types (second code fragment)?
  46.     2) Is it possible to make a declaration of a pointer to constant data
  47.        using the typdefed int_pointer?  If so, how?
  48.  
  49. Thanks in advance.
  50.  
  51. Shankha
  52. -- 
  53. -------------------------------------------------------------------------------
  54. Shankha S. Mitra                                               shankha@clsi.com
  55. CAD Language Systems, Inc.                      (410) 992-1716
  56. -------------------------------------------------------------------------------
  57.