Strange Behavior?
Shiping Zhang
ping at cubmol.bio.columbia.edu
Thu May 9 06:40:09 AEST 1991
In article <1991May8.020720.20170 at mccc.edu> pjh at 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.
-ping
More information about the Comp.lang.c
mailing list