TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: VICTOR KEMP
from: PETER HAYWOOD
date: 1998-01-31 01:42:00
subject: Re: converting char to string

Groovy hepcat Victor Kemp jived with All on 13 Jan 98  15:05:52!
converting char to string's a cool scene. Dig it!
 VK> Hi, how do you convert a char to a string?
  You don't! A char is a single char, while a string is a zero
terminated array of char. You can copy a char into a string, though.
 VK> I would like to concatenate a single char onto the end of a string but
 VK> it says it can't convert int to char * when I do this: 
 VK> char charvar;
 VK> char stringvar[90];
 VK> strcat(stringvar, charvar);
  strcat() takes two pointer arguments, the first being the
destination string, the second being the source string. You cannot
pass a single char to strcat().
  The error message about converting an int to char * means that a
char is a type of integer, and cannot portably be converted to a
pointer.
  There may be a way around this. Instead of using strcat(), use
strncat(), pass the address of the char you want to add to the string,
and pass 1 as the number of characters to copy. Eg.:
char charvar;
char stringvar[90];
...
strncat(stringvar, &charvar, 1);
  But you'd be better off not doing this at all. What you should do
instead, since you're only copying a single char, is find the end of
the string, copy the character there, and append a null character,
eg.:
char charvar;
char stringvar[90];
size_t len;
...
len = strlen(stringvar);
stringvar[len - 1] = charvar;
stringvar[len] = '\0';
                                                  Wolvaen
... I asked for the ketchup! I'm eating salad here! - Homer
--- Blue Wave/RA v2.20
---------------
* Origin: The Gate, Melbourne Australia, +61-3-9809-5097 33.6k (3:633/159)

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