Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!columbia!cs!d-yang From: d-yang@cs.columbia.edu (David Yang) Newsgroups: comp.lang.c Subject: Re: Help... Summary: Need to allocate memory for the array Message-ID: <372@cs.columbia.edu> Date: 10 Oct 89 03:23:37 GMT References: <731@carroll1.UUCP> <39902@bu-cs.BU.EDU> Organization: Columbia University Department of Computer Science Lines: 30 > dnewton@carroll1.UUCP (Dave 'Yes, I'm weird' Newton) said: > > Dave> Why doesn't this work? > > Dave> ========================== > Dave> #include > 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@cs.columbia.edu the