TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID VAN HOOSE
from: MATHIEU BOUCHARD
date: 1998-04-17 18:13:00
subject: reading input from stdin (was: I`m an id18:13:1204/17/98

   RA>> char name[40];
   RA>> gets(name);
 DVH> If you do that verbatim, you will have an overflow problem with all
 DVH> known compilers.  This is how that should be:
 DVH> char name[40];
 DVH> gets(name);
 DVH> name[40]='\n';
 DVH> You have to put a NULL on the end or it will have an overflow problem.
 DVH> I believe you have to do that with 'fgets' as well..
 DVH> Just put the null character on the very end of the array.
 DVH> Hope you like my advice. Peace!
That's WORSE. Sorry.
'\n' is not null, it's EOL (end of line)
name[40] is the first byte AFTER the array.
and it doesn't prevent gets from writing like 200 characters, or even
42.
fgets has _no_problem_ with that, though -- if you set the maximum
number of characters properly.
matju
--- Terminate 4.00/Pro
---------------
* Origin: The Lost Remains Of SatelliteSoft BBS (1:163/215.42)

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