Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!att!princeton!njsmu!mccc!pjh From: pjh@mccc.edu (Pete Holsberg) Newsgroups: comp.lang.c Subject: Re: Strange Behavior? Message-ID: <1991May9.161425.17079@mccc.edu> Date: 9 May 91 16:14:25 GMT References: <1991May8.020720.20170@mccc.edu> <1991May8.204009.1694@cubmol.bio.columbia.edu> Organization: The College On The Other Side Of Route One Lines: 37 In article <1991May8.204009.1694@cubmol.bio.columbia.edu> ping@cubmol.bio.columbia.edu (Shiping Zhang) writes: =In article <1991May8.020720.20170@mccc.edu> pjh@mccc.edu (Peter J. Holsberg) writes: =>Here is an extract from a program a student wrote. Note that he has =>declared "input" incorrectly. The strange behavior is that, when choice =>"1" is made, the print function outputs all but the first line that was =>entered. Can anyone explain that in terms of what scanf() is doing to =>memory near "input"? (This is on a 386, if endianism matters.) = =[...] = => char sentence [MAX][SIZE]; /* an array of strings */ => char *point[MAX]; /* an array of pointers to char */ => char *orginal[MAX]; /* an array holding the orginal sequence */ => char input; /* SHOULD HAVE BEEN int input!!! */ = =[...] = => printf("Make a choice: "); => scanf ("%d" , &input); /* value converted to decimal integer = =I think it results from declaring input as char and using it as int =in scanf(). When scanf() writes 1 into the location of input, it puts =into 0's those bits that belong to the first byte(s) of sentence[0], =terminating sentence[0] at its first byte. I could be wrong, though. I'm sure you're right. But what I really want to know is why the compiler assigns memory that way. It would seem to me that it would assign memory "downward," so that the attempt to write a 4 byte value into a 1 byte location would result in the "corruption" of the 3 cells *following* the one allocated to "input." Pete -- Prof. Peter J. Holsberg Mercer County Community College Voice: 609-586-4800 Engineering Technology, Computers and Math UUCP:...!princeton!mccc!pjh 1200 Old Trenton Road, Trenton, NJ 08690 Internet: pjh@mccc.edu Trenton Computer Festival -- 4/20-21/91