[comp.sys.sgi] uncanny casting coercion

root@MCIRPS2.MED.NYU.EDU (05/31/90)

Some observation about typing in sgi's compiler:

Observation 1)
..............
char *fgets(s,n,stream); et al return EOF on end of file and a NULL pointer
in s . The compiler will not allow a test

if(EOF==fgets(s,n,stream)) { ...

I had to do

if((char *)EOF==fgets(s,n,stream)) { ...

The MAN says :
--------------------
DIAGNOSTICS
     If end-of-file is encountered and no characters have been read, no
     characters are transferred to s and a NULL pointer is returned.  If a
     read error occurs, such as trying to use these functions on a file that
     has not been opened for reading, a NULL pointer is returned.  Otherwise s
     is returned.

-----------------------

Should I test s or fgets ?
Why does fgets return a char * ?


Observation 2)
..............

An ANSI C char decl:

MyFunction(char *Arg)
{
if(arg[0]==NULL)CallError();
}

VS:

MyFunction(char Arg[])
{
if(arg[0]==NULL)CallError();
}

A K&R 1 char decl:

MyFunction(Arg)
char *Arg;
{
if(arg[0]==NULL)CallError();
}

MyFunction(Arg)
char Arg[];
{
if(arg[0]==NULL)CallError();
}

The ANSI form MyFunction(char *Arg) produces a compiler complaint
on the == test.


Which is the most apopropriate form to use, or am I missing somthing.  Why
should the compiler complain, or is ansi typeing of pointers, arrays,and array
pointers different in a basic way ?

Cheers,


Dan.
c




--
+-----------------------------------------------------------------------------+
| karron@nyu.edu                          Dan Karron                          |
| . . . . . . . . . . . . . .             New York University Medical Center  |
| 560 First Avenue           \ \    Pager <1> (212) 397 9330                  |
| New York, New York 10016    \**\        <2> 10896   <3> <your-number-here>  |
| (212) 340 5210               \**\__________________________________________ |
+-----------------------------------------------------------------------------+

moss@BRL.MIL ("Gary S. Moss", VLD/VMB) (06/01/90)

< Observation 1)
< ..............
< char *fgets(s,n,stream); et al return EOF on end of file
No they don't, they return NULL.

< and a NULL pointer in s.
Wrong again.  This is not even possible; s should point to storage, this
cannot be changed by fgets.  Characters can be transferred to the storage
location whose address is contained in s, but that address (the value of
s) cannot be changed by fgets.  If this is confusing, get some C pointer
training before you do any further programming.

< The compiler will not allow a test
< 
< if(EOF==fgets(s,n,stream)) { ...
Well, at least it gets this right. ;-b

< I had to do
< 
< if((char *)EOF==fgets(s,n,stream)) { ...
Will you forget EOF already.  EOF is not a pointer value and has no meaning
in that context.

< DIAGNOSTICS
<      If end-of-file is encountered and no characters have been read, no
								       ^^
<      characters are transferred to s and a NULL pointer is returned.  If a
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
<      read error occurs, such as trying to use these functions on a file that
<      has not been opened for reading, a NULL pointer is returned.  Otherwise
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
<      s is returned.

You better RTFM *again*. ;-)

Good luck!