🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5174

Re: switch (expression)

#5174
From: Ray Butterworth
Date: Thu, 14 Jul 1988 15:19
23 lines
1338 bytes
In article <59881@sun.uucp>, guy@gorodish.Sun.COM (Guy Harris) writes:
> As such, I would avoid all pointer casts except the unavoidable ones (such as
> the -1 to "result of 'sbrk' type" casts).

To anyone that is documenting such functions:

Please be explicit about how to test the return value.

The man pages almost always say "returns -1 ...",
and too often I've seen code that tests the value with
something like:       if ((int)function() == -1) ...

This happens to work sometimes, but it is still wrong.
It should be:         if (function() == (type*)-1) ...

The man page should explicitly mention this cast,
and not assume that it is obvious to everyone.

(To see that it should be obvious, consider the source for
 the function.  It must "return (type*)-1;", so you want to
 compare the result with (type*)-1.  Doing it the other way
 compares -1 with (int)(type*)-1, and there is no guarantee
 that the double cast will result in -1.)
Technical Details
Message-ID: <19876@watmath.waterloo.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!rbutterworth
References: <1988Jul12.105547.13268@light.uucp> <755@vsi.UUCP> <59881@sun.uucp>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!rbutterworth
From: rbutterworth@watmath.waterloo.edu (Ray Butterworth)
Newsgroups: comp.std.c
Subject: Re: switch (expression)
Message-ID: <19876@watmath.waterloo.edu>
Date: 14 Jul 88 15:19:43 GMT
References: <1988Jul12.105547.13268@light.uucp> <755@vsi.UUCP> <59881@sun.uucp>
Organization: U of Waterloo, Ontario
Lines: 23