Help...
David Yang
d-yang at cs.columbia.edu
Tue Oct 10 13:23:37 AEST 1989
> dnewton at carroll1.UUCP (Dave 'Yes, I'm weird' Newton) said:
>
> Dave> Why doesn't this work?
>
> Dave> ==========================
> Dave> #include <stdio.h>
> Dave> main ()
> Dave> {
> Dave> char h[];
> Dave> scanf ("%s", h);
> Dave> printf ("%s\n", h);
> Dave> }
> Dave> ==========================
>
char h[] doesn't reserve any memory for the array.
Try char h[some constant for the max str. length expected]
e.g., char h[80];
You can see the difference if you "cc -S" both versions and use "diff",
or whatever file comparing command your system has,
on the .s (assembly language) files created.
Note that declarations like "char h[]" are okay for parameters of functions
since in that case they are treated just like a pointer (i.e., char *h)
since when an array is an argument in a function call, it's address gets
passed.
Hope this was somewhat comprehensible,
David Yang
d-yang at cs.columbia.edu
the
More information about the Comp.lang.c
mailing list