Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!rutgers!att!cbnewsi!npl From: npl@cbnewsi.ATT.COM (nickolas.landsberg) Newsgroups: comp.lang.c Subject: Re: Help... Summary: Wrong! Message-ID: <902@cbnewsi.ATT.COM> Date: 10 Oct 89 14:37:19 GMT References: <731@carroll1.UUCP> <39902@bu-cs.BU.EDU> Organization: AT&T Bell Laboratories Lines: 28 In article <39902@bu-cs.BU.EDU>, austin@bucsf.bu.edu (Austin Ziegler) writes: > dnewton@carroll1.UUCP (Dave 'Yes, I'm weird' Newton) said: > Dave> Why doesn't this work? > Dave> #include > Dave> main () > Dave> { > Dave> char h[]; > Dave> scanf ("%s", h); > Dave> printf ("%s\n", h); > Dave> } > Dave> It seems innocent enuf, but just prints garbage. I'm missing something > Dave> obvious, but I'll be darned if I know what it is. > I don't know, but I just tried it. One possibility is to strcat a \0 > to the end of h before printf'ing h. Otherwise, you can get the same > result from char *h, and not get too many problems. PLEASE! Don't post an answer unless you know what the (expletive deleted) you're talking about! Neither char h[] nor char *h reserve any storage for the data. IMHO, the compiler which allowed an automatic to be used in such a way is brain-dead, but that's another issue. The first poster got garbage, as he should have. The fact that it worked for the second poster was unfortunate, at best. As an aside, scanf() DOES null-terminate strings, therefore the strcat() "solution" is bogus. (Did you ever stop to think of how strcat knows where the end of the original string is?) Grumpy