TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JOHN RICHARDSON
from: AUKE REITSMA
date: 1998-04-05 19:48:00
subject: reading input from stdin (was: I`m an id19:48:0004/05/98

Hi John,
On 03 Apr 98, 10:12, you wrote to David Van Hoose
 DVH>>> char name[40];
 DVH>>> gets(name);
 DVH>>> name[40]='\0';
 >>
 TT>>> That still leaves the problem that you're writing to memory
 TT>>> that isn't yours. Your array is only 40 characters long and
 TT>>> you try to access the 41st element!
 >>
 >> Actually it is correct.  I don't know where you see anything
 >> wrong,
 JR> I think Tom means that name[40]='\0' should be name[39]='\0'
 JR> since C/C++ is zero based (i.e 0 is always the first element
 JR> hence 39 is the fortieth and 40 is the fortyfirst).
__AND__ the problem that gets() does write outside the forty byte array if 
the user enters more than 39 characters!
Avoid gets()  AT __ALL__ COST !!!
Greetings from
 _____
 /_|__|  Auke Reitsma, Delft, The Netherlands.
/  | \  --------------------------------------
--- GEcho 1.00
---------------
* Origin: Home by the C (Auke.Reitsma@net.hcc.nl) (2:281/400.20)

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™.