Help

Gregory Smith greg at utcsri.UUCP
Tue Nov 18 03:40:48 AEST 1986


In article <408 at ethz.UUCP> lubich at ethz.UUCP (Hannes Lubich) writes:
>Well, I'm trapped.
>When using something like :
> while (fgets (teststring, 100, myfptr) != NULL)
... bombs on return...
>Furthermore when I declare teststring as : char teststring[];
>the above result appears but when I try to declare it as
>char *teststring; I get a 'Bus error' at once.
>
>Could somebody give me a hint about that misterious 'fgets' ?

Did you try 'char teststring[100]'? fgets doesn't allocate storage for
you.  When you declare 'char teststring[];', you get a zero-byte array,
and then pass its address to fgets, meanwhile you are telling fgets
that there are 100 bytes there. So fgets scribbles all over your stack
frame, and the return address is destroyed.  If you say 'char
*teststring', you are passing the pointer itself to fgets without
initializing it to anything. So fgets writes the string to an undefined
area.
-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...



More information about the Comp.lang.c mailing list