[LCP]Very Small Doubt...

Muli Ben-Yehuda mulix at actcom.co.il
Mon Jul 8 20:18:05 UTC 2002


On Tue, Jul 09, 2002 at 02:28:04AM +0530, Goverthanan wrote:
> hi all,
> this is a very small doubt in c.
> On execution the following program throws segmentation fault error.
> 
> #include <stdio.h>
> #include <string.h>
> 
> main()
> {
> int n;
> char *str, *str1; <---- you declare two "pointers to char", but they
                          dont point anywhere yet (or rather, they
			  point at some random memory location)

> scanf("%s", str); <----- and now you read into them, therefore
                           scribbling all over that memory location. 
> scanf("%s", str1);
> scanf("%d",&n);
> printf("%d\n", bcmp(str, str1, n));
> }
> 
> Instead when the order of variable declaration is changed, program worked
> fine.

that was a fluke. 

> May i know what makes the diffenence in the order of variable
> declarations.

Better get a good C book on pointers and memory management. K&R's The
C Programming Language is recommended. 
-- 
http://vipe.technion.ac.il/~mulix/
http://syscalltrack.sf.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20020708/36cf4dfb/attachment.pgp 


More information about the linuxCprogramming mailing list