uncanny casting coercion

root at MCIRPS2.MED.NYU.EDU root at MCIRPS2.MED.NYU.EDU
Thu May 31 08:31:44 AEST 1990


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 at 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               \**\__________________________________________ |
+-----------------------------------------------------------------------------+



More information about the Comp.sys.sgi mailing list