"do ... while ((NULL + 1) - 1);" -- valid C?

Wolfgang Deifel wolfgang at ruso.UUCP
Tue Aug 15 20:26:56 AEST 1989


ccdn at levels.sait.edu.au (DAVID NEWALL) writes:

>                while ((s = strchr(s, ',') + 1) - 1);

>I've been told that this is not valid C because, in the case that there
>are no more fields (commas), strchr() returns NULL; and NULL + 1 is not
>valid.

I think it's a difference if you write " NULL + 1 " ( which is non-
portable C, NULL is a machine dependent macro ) and " strchr(...) + 1 ".
strchr() is a function that returns always a legal value. If strchr fails
it will return (char*)0 ( regardless of the machine or the compiler ),
and here it's legal to add '1' ( the result is (char*)1 ).

----------------------------------------------------------------------------
Wolfgang Deifel
Dr. Ruff Software GmbH, 5100 Aachen, Juelicherstr. 65-67, W-Germany
uucp: ...!uunet{!mcvax}!unido!rwthinf!ruso!wolfgang - phone : +49 241 156038



More information about the Comp.lang.c mailing list