[comp.lang.c] BSD C compiler bug

chris@mimsy.UUCP (Chris Torek) (03/21/88)

In article <570@mtxinu.UUCP> tim@mtxinu.UUCP (Tim Wood) writes:
>#define NULL 0
	...
>	unsigned char	**foo;
>
>	foo = NULL;
>	foo = (NULL, NULL);
>	foo = ( !foo ? NULL : (fn(2), NULL) );

[produces]
>"comma.c", line 8: warning: illegal combination of pointer and integer, op =
>"comma.c", line 9: warning: illegal combination of pointer and integer, op =

>K&R say that the result type & value of a comma expression are that of the
>second term, which is NULL (0) in all cases here.  And [0] is assignment
>compatible to all pointers.

I am not convinced it is a bug.  `The integer constant 0' can be
assigned to any pointer; the question is whether the result of a
complex expression can be considered an `integer constant'.

The 11 Jan 1988 draft of the dpANS has this to say about constant
expressions (p. 56):

    CONSTRAINTS
       Constant expressions shall not contain assignment, increment,
    decrement, function-call, or comma operators, except when they are
    contained within the operand of a |sizeof| operator.

That disallows the second expression (foo = (NULL, NULL)) explicitly,
and the third because of the function call; it does not disallow

	foo = 1 ? NULL : NULL;

but the latest pcc seems to allow this anyway.

I do think prohibiting comma expressions in constants is unnecessary.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

gwyn@brl-smoke.ARPA (Doug Gwyn ) (03/21/88)

In article <10747@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
-In article <570@mtxinu.UUCP> tim@mtxinu.UUCP (Tim Wood) writes:
->	unsigned char	**foo;
->	foo = (NULL, NULL);
->	foo = ( !foo ? NULL : (fn(2), NULL) );
-The 11 Jan 1988 draft of the dpANS has this to say about constant
-expressions (p. 56):
...
-That disallows the second expression (foo = (NULL, NULL)) explicitly,
...

What do constant expressions have to do with it?  If this were a
static initializer I could see the relevance, but it's not.  The
reported behavior is simply a bug.

chris@mimsy.UUCP (Chris Torek) (03/22/88)

>In article <10747@mimsy.UUCP> I said
[	char *p; p = (0, 0); ]
>>The 11 Jan 1988 draft of the dpANS [disallows `,' in] constant
>>expressions (p. 56):

In article <7507@brl-smoke.ARPA> gwyn@brl-smoke.ARPA (Doug Gwyn) writes:
>What do constant expressions have to do with it?

Constant expressions have *everything* to do with it!

How do you create a nil pointer of type T in C?  Answer: put the
integer constant `0' in a pointer context (assignment or comparison, or
[dpANS] argument to a function that has a prototype in scope).  What is
`the integer constant 0'?  Clearly this is any constant expression
whose value is zero and whose type is one of the integral types {char,
short, int, long} or their signed or unsigned variants.  The
expression

	(0, 0)

certainly has the value zero and an integral type, but according to
the dpANS, it is not a constant expression.  Hence it cannot be `the
integer constant zero'; it is merely an integer expression whose value
is zero.  The situation is thus the same as if one were to write

	char *p;
	int zero = 0;
	p = zero;

I have not found anything in the draft standard to contradict
this reasoning.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

gwyn@brl-smoke.ARPA (Doug Gwyn ) (03/23/88)

In article <10754@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>Constant expressions have *everything* to do with it!

Ok, I see now.  The relevant factor is that integer-to-pointer
conversion does not occur automatically for assignment, except
in the case that the RHS is a null pointer constant, and as
you correctly observer, (0,0) is not a valid null pointer constant,
because the comma operator is not allowed in constant expressions
(except within the operand of sizeof).  I'm not sure why that
constraint was imposed; it doesn't seem necessary.