Help...

Richard Childers childers at avsd.UUCP
Thu Oct 12 03:28:06 AEST 1989


dnewton at carroll1.cc.edu (Dave 'Yes, I'm weird' Newton) writes:

>Why doesn't this work?
>
>#include <stdio.h>
>main ()
>{
>   char      h[];
>   scanf ("%s", h);
>   printf ("%s\n", h);
>}

I compiled it and ran it without problems, on a Sun 3/50 running SunOS 3.5.
It did complain about a segmenttation fault and dump core, though. Probably
related to the fact that nowhere is the size of h[] defined. I defined it
and the problem went away.

If you think about it from the compiler's point of view, it makes sense. It
allocates space as it is told to. If you don't allocate space, it might or
might not make an educated guess, hand you a predetermined buffer of a fixed
size, or hand you nothing but a pointer to a single byte, assuming you mean
to define h[] as h[1].

>  It seems innocent enuf, but just prints garbage.  I'm missing something
>obvious, but I'll be darned if I know what it is.

Hope this is of assistance ...

>David L. Newton       |      dnewton at carroll1.UUCP     | Quote courtesy of
>(414) 524-7343 (work) |     dnewton at carroll1.cc.edu    | Marie Niechwiadowicz,
>(414) 524-6809 (home) | 100 NE Ave, Waukesha, WI 53186 | Boston College.

 -- richard


-- 
 *	A CITIZEN:   "Who might you be ? Samson ? --"                         *
 *	CYRANO:      "Precisely. Would you kindly lend me your jawbone ?"     *
 *                    from _Cyrano de Bergerac_, by Edmond Rostand            *
 *        ..{amdahl|decwrl|octopus|pyramid|ucbvax}!avsd.UUCP!childers         *



More information about the Comp.lang.c mailing list