home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!math.fu-berlin.de!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!jutta
- From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
- Subject: Re: "const" Parameter compatibility
- Message-ID: <1993Jan7.192557.9594@cs.tu-berlin.de>
- Keywords: prototypes const
- Sender: news@cs.tu-berlin.de
- Organization: Techn. University of Berlin, Germany
- References: <1993Jan7.154448.19249@eplrx7.es.duPont.com>
- Date: Thu, 7 Jan 1993 19:25:57 GMT
- Lines: 58
-
- hanafey@eplrx7.es.dupont.com (Mike Hanafey) writes:
- > Are the compiler messages below a bug, or is this correct ANSI C?
-
- The compiler is right, the code is incorrect. (I would like it to be
- correct, personally, but ANSI decrees otherwise. Does anyone know why?)
-
- In short, the standard allows assigning a pointer-to-foo to a
- pointer-to-const|volatile-foo, but that rule goes only one
- level deep, it's not recursive.
-
- Here are three lines of your code again, and one of the warnings.
-
- > 1 int funca(const int **a)
- > 10 int *b;
- > 15 (void) funca(&b);
- > "test.c", line 15: warning: argument is incompatible with prototype: arg #1
-
- An ANSI C compiler must issue a warning when you call a function
- whose prototype is in scope ("funca") with an argument ("&b") that
- could not be assigned to an unqualified version of the parameter
- in the prototype ("const int ** a").
-
- (To avoid confusions later, in
-
- const int ** a
-
- it is not the "a" that is qualified by the const, it's the thing the
- pointer it points to points to. So an "unqualified version of the
- parameter" is still
-
- const int ** a)
-
- When can you assign to this "a"? You can assign two pointers to
- object if
-
- "both operands are pointers to qualified or unqualified
- versions of compatible types, and the type pointed to by
- the left has all the qualifiers of the type pointed to
- by the right."
- [ANSI C, 3.3.16, Assignment Operators]
-
- This means you can add qualifiers to the things-pointed-to,
- but can't strip them off easily. Very nice, except that it's not
- recursive -- once you're talking about "the type pointed to",
- you're stuck with type compatibility.
-
- Type compatibility is very strict. Pointers are compatible if
- they point to compatible types, and types are compatible only if
- they have the same qualifiers, so
-
- int *
- and
- const int *
-
- are not compatible; all that made assignments between them work
- was the special rule above.
-
- Jutta (jutta@cs.tu-berlin.de)
-