[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [LCP]Very Small Doubt...



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/

Attachment: pgp00000.pgp
Description: PGP signature