TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID VAN HOOSE
from: ROGER SCUDDER
date: 1998-04-02 15:41:00
subject: reading input from stdin (was: I`m an id15:41:2604/02/98

Hello David.
01 Apr 98 15:19, David Van Hoose wrote to Tom Torfs:
DVH>> char name[40];
DVH>> gets(name);
DVH>> name[40]='\0';
TT>> That still leaves the problem that you're writing to memory that
TT>> isn't yours. Your array is only 40 characters long and you try to
TT>> access the 41st element!
DVH> Actually it is correct.  I don't know where you see anything wrong, but
DVH> I have been making programs with that method for a while.
DVH> Here is how I thought of it:
 You missed the point here, David.  I'm sure you know this, but it
 slipped past you.
 An array is base 0, meaning that the first subscript is 0.
 When you declare an array like this:
 char name[40];
 you have reserved 40 elements, the first being  name[0] and the
 last being  name[39].
 When you place anything in name[40] you have gone out of bounds.
 The NUL character belongs in  name[39].
 Does any of this ring a bell?
 -Roger
--- Msged/386 4.20 beta 3kl3
(1:273/404@fidonet)
---------------
* Origin: Box Of Rotting Corpses BBS, Upper Darby, PA, USA

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