Date: Wed, 15 Jan 1997 08:40:01 -0800 (PST) From: davidn@unique.usn.blaze.net.au (David Nugent) To: freebsd-bugs Subject: Re: bin/2502: Unable to sscanf first integer value. Message-ID: <199701151640.IAA10123@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/2502; it has been noted by GNATS. From: davidn@unique.usn.blaze.net.au (David Nugent) To: scrutchfield@ifusion.com Cc: freebsd-gnats-submit@freebsd.org Subject: Re: bin/2502: Unable to sscanf first integer value. Date: Thu, 16 Jan 1997 03:23:01 +1100 scrutchfield@ifusion.com writes: > I am unable to sscanf correctly 2 integers from a string. A Sample > program that recreates the problem is shown below. This is a problem > in both libc and libc_r. No, it is a problem with your code, not libc. > #include <stdio.h> > #include <string.h> > #include <stdlib.h> > > main() > { > char *tmp = "999 12346"; > char *ptr; > unsigned short x; > unsigned short y; > unsigned short z; > unsigned int a; > int result; > > result = sscanf ( tmp, "%d %d", &x, &y ); You're scanning integers, and this is what sscanf expects. You're passing it pointers to 16-bit, not 32-bit, quantities. 'x' is "zeroed" when the value 12346 (32-bit) is written to y, as the following fprintf() shows. > z = strtol ( tmp, &ptr, 0 ); > a = atoi ( tmp ); And I assume this is supposed to be 'ptr', not 'tmp'. > (void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a ); > exit ( 0 ); > } The following is a "fixed" version of your code which simply changes the integral variables to the 32-bit quantities that sscanf expects, which works fine. #include <stdio.h> #include <string.h> #include <stdlib.h> main() { char *tmp = "999 12346"; char *ptr; int x; int y; int z; int a; int result; result = sscanf ( tmp, "%d %d", &x, &y ); z = strtol ( tmp, &ptr, 0 ); a = atoi ( ptr ); (void)fprintf ( stderr, "x(%d)y(%d)z(%d)a(%d)\n", x, y, z, a ); exit ( 0 ); } Regards, David Nugent - Unique Computing Pty Ltd - Melbourne, Australia Voice +61-3-9791-9547 Data/BBS +61-3-9792-3507 3:632/348@fidonet davidn@freebsd.org davidn@blaze.net.au http://www.blaze.net.au/~davidn/
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199701151640.IAA10123>