incompatibility of character pointers

Bruce Blodgett blodgett at granite.cr.bull.com
Wed Feb 7 03:39:33 AEST 1990


As far as I can tell, the following code fragment is not legal ANSI C:
{
  char * cp;
  unsigned char * ucp;
  signed char * scp;

  cp = "abc"; /* ok - string literal (array of char) becomes pointer to char */
  ucp = "def"; /* pointer to unsigned char INCOMPATIBLE with pointer to char */
  scp = "ghi"; /* pointer to   signed char INCOMPATIBLE with pointer to char */
}
Constraint from simple assignment section 3.3.16.1:
"One of the following shall hold:
... both operands are pointers to ... compatible types"

Now consider the types being pointed to (unsigned char vs char vs signed char).

>From compatible type section 3.1.2.6:
"Two types have compatible type if their types are the same.  Additional rules
for determining whether two types are compatible are described in section
3.5.2 for type specifiers"

>From type specifier section 3.5.2:
"Each list of type specifiers shall be one of the following sets"
Since char, signed char, and unsigned char form three distinct lists, they
are not the same type.  Consequently, pointers to them are NOT compatible
types.  Am I wrong?

Bruce Blodgett



More information about the Comp.std.c mailing list