TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID VAN HOOSE
from: ERIK STEEN JAKOBSEN
date: 1998-04-02 23:28:00
subject: Reading Input From Stdin

Hello David!
Replying to a message of David Van Hoose to David Noon:
 DVH> -> DH>char name[41];
 DVH> -> DH>gets(name);
 DVH> -> DH>name[41]='\0';
 DVH> -> DH>printf("%s",name);
 DVH> ->
 DVH> -> DH>My bad.
 DVH> ->
 DVH> -> It still is, I'm afraid.
 DVH> ->
 DVH> -> An array of size 41 has subscripts 0 thru 40, so the 3rd line has
 DVH> a -> subscript out of bounds. -> -> Moreover, the gets() function
 DVH> does not limit the transfer to the size -> of the supplied buffer,
 DVH> since it does not know that size. The use of -> fgets() instead is
 DVH> much safer.
 DVH> 1: fgets() is for use with files.
   correct .-) BUT  you could point it towards stdin .-) 
   I'd use cin.getline .-) or any input function with max.length defined.
 DVH> 2: It works, and works perfectly if you ever bothered to try it.
  nope ... you are rescued by a specific stac-implementation which allocates 
in even sizes.
  if you either makes the array even or place it in the data-segment instead 
of in the 
 stack-segment then you'll see how wrong you are.-)
 DVH> 3: It counts 0 thru 41 standardly. using cin.get() it uses 0 thru 40   
 DVH> because it already accounts for the NULL on the end.
  pure nonsens... 
 DVH> Ok?  Try it.  You'll see that I am right. I use that all the time.
  then I don't want to use a plan you have programmed for .
  You are by definition ... WRONG.
Bye, ERIK!
--- FleetStreet 1.22 NR
---------------
* Origin: DataVisor BBS - TeamOS2 (2:238/52.136)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.