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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!math.fu-berlin.de!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!jutta
  3. From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
  4. Subject: Re: "const" Parameter compatibility 
  5. Message-ID: <1993Jan7.192557.9594@cs.tu-berlin.de>
  6. Keywords: prototypes const
  7. Sender: news@cs.tu-berlin.de
  8. Organization: Techn. University of Berlin, Germany
  9. References: <1993Jan7.154448.19249@eplrx7.es.duPont.com>
  10. Date: Thu, 7 Jan 1993 19:25:57 GMT
  11. Lines: 58
  12.  
  13. hanafey@eplrx7.es.dupont.com (Mike Hanafey) writes:
  14. > Are the compiler messages below a bug, or is this correct ANSI C?
  15.  
  16. The compiler is right, the code is incorrect.  (I would like it to be
  17. correct, personally, but ANSI decrees otherwise.  Does anyone know why?)
  18.  
  19. In short, the standard allows assigning a pointer-to-foo to a
  20. pointer-to-const|volatile-foo, but that rule goes only one
  21. level deep, it's not recursive.
  22.  
  23. Here are three lines of your code again, and one of the warnings.
  24.  
  25. >     1  int funca(const int **a)
  26. >    10      int *b;
  27. >    15      (void) funca(&b);
  28. > "test.c", line 15: warning: argument is incompatible with prototype: arg #1
  29.  
  30. An ANSI C compiler must issue a warning when you call a function
  31. whose prototype is in scope ("funca") with an argument ("&b") that
  32. could not be assigned to an unqualified version of the parameter
  33. in the prototype ("const int ** a").
  34.  
  35. (To avoid confusions later, in
  36.  
  37.        const int ** a
  38.  
  39. it is not the "a" that is qualified by the const, it's the thing the
  40. pointer it points to points to.  So an "unqualified version of the
  41. parameter" is still
  42.  
  43.        const int ** a)
  44.  
  45. When can you assign to this "a"?  You can assign two pointers to 
  46. object if 
  47.  
  48.     "both operands are pointers to qualified or unqualified
  49.     versions of compatible types, and the type pointed to by
  50.     the left has all the qualifiers of the type pointed to
  51.     by the right."
  52.             [ANSI C, 3.3.16, Assignment Operators]
  53.  
  54. This means you can add qualifiers to the things-pointed-to,
  55. but can't strip them off easily.  Very nice, except that it's not
  56. recursive -- once you're talking about "the type pointed to",
  57. you're stuck with type compatibility.
  58.  
  59. Type compatibility is very strict.  Pointers are compatible if
  60. they point to compatible types, and types are compatible only if
  61. they have the same qualifiers, so
  62.  
  63.     int *
  64. and
  65.     const int * 
  66.  
  67. are not compatible; all that made assignments between them work
  68. was the special rule above.
  69.  
  70. Jutta (jutta@cs.tu-berlin.de)
  71.