Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!ginosko!uunet!munnari.oz.au!cs.mu.oz.au!ok From: ok@cs.mu.oz.au (Richard O'Keefe) Newsgroups: comp.lang.c Subject: Re: Help... Message-ID: <2369@munnari.oz.au> Date: 10 Oct 89 03:27:45 GMT References: <731@carroll1.UUCP> <39902@bu-cs.BU.EDU> Sender: news@cs.mu.oz.au Lines: 29 In article <39902@bu-cs.BU.EDU>, austin@bucsf.bu.edu (Austin Ziegler) writes: > Dave> ========================== > Dave> #include > 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.