concatenating single chars to multi char arrays
Hi
I have a problem assigning a single character into a pointer and an array larger than 1 - I think I know why this is but I am unsure about how to overcome it...
Code:
char TempChar[3]
char SinChar[1];
// At any time StoreChar could contain 1 to 3 characters
// - I want whatever characters are stored in it to be assigned
//to *data - I think the interger cast causes problems but it is
//necessary - how can I convert TempChar to int without messing
//up the data?
*data= int(TempChar);
//Later on I wish to concatenate SinChar (a single char
// array) to TempChar - this seems to work but always has
// nonsense characters as well - I think this is due to a single
//character being concatenated to a array which could contain
//either 1 or 2 characters - how can I do this without the
//nonsense characters?
strcat(TempChar, SinCheck);
Any help much appreciated!
Jimbo
Somewhat faster and less artificial solution.
Code:
#define strcatchar(__str,__cc) \
{ \
int __slen; \
__str[__slen = strlen(__str)] = __cc; \
__str[__slen+1] = '\0'; \
}
Re: Somewhat faster and less artificial solution.
Quote:
Originally posted by qed
Code:
#define strcatchar(__str,__cc) \
{ \
int __slen; \
__str[__slen = strlen(__str)] = __cc; \
__str[__slen+1] = '\0'; \
}
Loose the __'s. They make the code almost unreadable.
Code:
#define strcatchar(str,cc) \
{ \
int slen; \
str[slen = strlen(str)] = cc; \
str[slen+1] = '\0'; \
}
will suffice.
and the line str[slen = strlen(str)] = cc; is a nice trick but obfuscates the routine. a simple
slen = strlen(str);
str[slen] = cc;
does the job just as well without the confusion.