Help...

Richard O'Keefe ok at cs.mu.oz.au
Tue Oct 10 13:27:45 AEST 1989


In article <39902 at bu-cs.BU.EDU>, austin at bucsf.bu.edu (Austin Ziegler) writes:
> Dave> ==========================
> Dave> #include <stdio.h>
> Dave> main ()
> Dave> {
> Dave>    char      h[];
> Dave>    scanf ("%s", h);
> Dave>    printf ("%s\n", h);
> Dave> }
> Dave> ==========================
> I don't know, but I just tried it.

I thought, "this is just too obvious, no point me replying".
Appears it's not obvious.

	char h[];

doesn't actually allocate any chars.  So where is the scanf() supposed
to put them?  Scanf doesn't allocate strings for you, you have to give
it pointers to blocks which already exist.  When I tried this with gcc
it said
	zabbo.c:4: array size missing in `h'
and it was absolutely right.  Declare
	#define MaxLineSize 512	/* or whatever you fancy */
	    char h[MaxLineSize];

Also, be very careful when using scanf().  After making the correction,
if the input file is "   foo   baz  \n" the output will be "foo\n".
You may find fgets() easier to drive.



More information about the Comp.lang.c mailing list