TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: MATHIEU BOUCHARD
from: JONATHAN DE BOYNE POLLARD
date: 1998-01-30 18:34:00
subject: converting char to string

 MB> way #1:
 MB> int foo = strlen(stringvar);
 MB> stringvar[foo]=charvar;
 MB> stringvar[foo+1]=0;
 MB> way #2:
 MB> char foo[2] = { charvar, 0 };
 MB> strcat (stringvar, foo);
 MB> i hope that this is correct.
I'm glad that I'm not the only one familiar with #2.  (-:
One slight niggle: The return value from strlen() is a `size_t' not an `int'. 
 On some platforms, notably 16-bit Intel x86 architectures using "huge" 
memory models, `size_t' is a wider type than `int' is.  
Also bear in mind that `size_t' is always an *unsigned* integer type, whereas 
`int' is signed by default.  Although it doesn't cause a problem in this 
particular case, it can cause problems with strings that are longer than 
INT_MAX characters long, especially if one does such things as 
        int len = strlen( /*...*/ ) ;
        if (len > 0) { /* ... */ }
.  Again, this error is more obvious on some architectures than on others.
 ¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)

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