Help...

nickolas.landsberg npl at cbnewsi.ATT.COM
Wed Oct 11 00:37:19 AEST 1989


In article <39902 at bu-cs.BU.EDU>, austin at bucsf.bu.edu (Austin Ziegler) writes:
> dnewton at carroll1.UUCP (Dave 'Yes, I'm weird' Newton) said:
> Dave> Why doesn't this work?
> Dave> #include <stdio.h>
> 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



More information about the Comp.lang.c mailing list