Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!unido!rwthinf!ruso!wolfgang
From: wolfgang@ruso.UUCP (Wolfgang Deifel)
Newsgroups: comp.lang.c
Subject: Re: "do ... while ((NULL + 1) - 1);" -- valid C?
Message-ID: <828@ruso.UUCP>
Date: 15 Aug 89 10:26:56 GMT
References: <1043@levels.sait.edu.au>
Organization: Dr. Ruff Software GmbH, Aachen, W.Germany
Lines: 18

ccdn@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