Author |
Message |
Tom Hosia #1 / 10
|
 converting char to pointer char
I wanna call strcat to append a character ch to a string str, but the 2nd param has to be a pointer so how would I do this if I didn't want to make ch a character pointer at declaration time char str[256]; char ch; /* wanna do this but can't cause ch is not a pointer */ strcat(str, ch);
|
Thu, 29 Apr 2004 11:04:50 GMT |
|
 |
Eric Go #2 / 10
|
 converting char to pointer char
Quote:
> I wanna call strcat to append a character ch to a string str, but the 2nd > param has to be a pointer so how would I do this if I didn't want to make ch > a character pointer at declaration time > char str[256]; > char ch; > /* wanna do this but can't cause ch is not a pointer */ > strcat(str, ch);
ch isn't a string either, so that will cause you problems as well. you could try: char str[256]; char ch[2]; /* you need two elements...one for the character, */ /* and one for the null terminator */ strcat( str, ch ); -- == Eric Gorr =========================================== ICQ:9293199 === "Therefore the considerations of the intelligent always include both benefit and harm." - Sun Tzu == Insults, like {*filter*}, are the last refuge of the incompetent... ===
|
Thu, 29 Apr 2004 11:33:17 GMT |
|
 |
pete #3 / 10
|
 converting char to pointer char
Quote:
> I wanna call strcat to append a character ch to a string str, but the 2nd > param has to be a pointer so how would I do this if I didn't want to make ch > a character pointer at declaration time > char str[256]; > char ch; > /* wanna do this but can't cause ch is not a pointer */ > strcat(str, ch);
/* BEGIN new.c */ #include <stdio.h> #include <string.h> int main(void) { char str[256] = "hello, worl"; char ch = 'd'; size_t length; length = strlen(str); str[length++] = ch; str[length] = '\0'; puts(str); return 0; Quote: }
/* END new.c */ -- pete
|
Thu, 29 Apr 2004 11:39:51 GMT |
|
 |
Ben Pfaf #4 / 10
|
 converting char to pointer char
Quote:
> I wanna call strcat to append a character ch to a string str, but the 2nd > param has to be a pointer so how would I do this if I didn't want to make ch > a character pointer at declaration time > char str[256]; > char ch; > /* wanna do this but can't cause ch is not a pointer */ > strcat(str, ch);
A few approaches: /* Convert character to string, then concatenate string. */ char str2[2]; str2[0] = ch; str2[1] = '\0'; strcat (str, str2); /* Concatenate character to string directly. */ char *p; p = strchr (str, '\0'); p[0] = ch; p[1] = '\0'; /* Devious. */ sprintf (strchr (str, '\0'), "%c", ch); -- "The fact that there is a holy war doesn't mean that one of the sides doesn't suck - usually both do..." --Alexander Viro
|
Thu, 29 Apr 2004 11:37:33 GMT |
|
 |
Kohn Emil Da #5 / 10
|
 converting char to pointer char
Quote:
> > I wanna call strcat to append a character ch to a string str, but the 2nd > > param has to be a pointer so how would I do this if I didn't want to make ch > > a character pointer at declaration time > > charstr[256]; > > char ch; > > /* wanna do this but can't cause ch is not a pointer */ > > strcat(str, ch); > A few approaches:
<snipped> And yet another one: strncat(str,&ch,1); Emil
|
Thu, 29 Apr 2004 13:23:05 GMT |
|
 |
-hs- #6 / 10
|
 converting char to pointer char
Quote: >> > I wanna call strcat to append a character ch to a string str, but >> > the 2nd param has to be a pointer so how would I do this if I didn't >> > want to make ch a character pointer at declaration time >> > charstr[256]; >> > char ch; > strncat(str,&ch,1);
An interesting use of strncat(). Thanks. -- -hs- emdel at noos.fr "support Afghans against Taleban" "Car les bandits qui sont cause des guerres N'en meurent jamais, on n'tue qu'les innocents." Gaston Monthus -- La Butte Rouge
|
Thu, 29 Apr 2004 17:37:28 GMT |
|
 |
Serve La #7 / 10
|
 converting char to pointer char
Quote: > I wanna call strcat to append a character ch to a string str, but the 2nd > param has to be a pointer so how would I do this if I didn't want to make ch > a character pointer at declaration time > char str[256]; > char ch; > /* wanna do this but can't cause ch is not a pointer */ > strcat(str, ch);
size_t len = strlen(str); buf[len] = ch; buf[++len] = '\0';
|
Thu, 29 Apr 2004 20:11:05 GMT |
|
 |
Tom Hosia #8 / 10
|
 converting char to pointer char
Thanks, I like this solution the best...short and quick!!! Tom
Quote:
> > > I wanna call strcat to append a character ch to a string str, but the 2nd > > > param has to be a pointer so how would I do this if I didn't want to make ch > > > a character pointer at declaration time > > > charstr[256]; > > > char ch; > > > /* wanna do this but can't cause ch is not a pointer */ > > > strcat(str, ch); > > A few approaches: > <snipped> > And yet another one: > strncat(str,&ch,1); > Emil
|
Thu, 29 Apr 2004 22:13:50 GMT |
|
 |
Jack Klei #9 / 10
|
 converting char to pointer char
in comp.lang.c: Quote:
> > I wanna call strcat to append a character ch to a string str, but the 2nd > > param has to be a pointer so how would I do this if I didn't want to make ch > > a character pointer at declaration time > > char str[256]; > > char ch; > > /* wanna do this but can't cause ch is not a pointer */ > > strcat(str, ch); > A few approaches: > /* Convert character to string, then concatenate string. */ > char str2[2]; > str2[0] = ch; > str2[1] = '\0'; > strcat (str, str2); > /* Concatenate character to string directly. */ > char *p; > p = strchr (str, '\0'); > p[0] = ch; > p[1] = '\0'; > /* Devious. */ > sprintf (strchr (str, '\0'), "%c", ch);
Not only devious, but undefined. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Fri, 30 Apr 2004 05:05:38 GMT |
|
 |
Ben Pfaf #10 / 10
|
 converting char to pointer char
Quote:
> in comp.lang.c:
> > > I wanna call strcat to append a character ch to a string str, but the 2nd > > > param has to be a pointer so how would I do this if I didn't want to make ch > > > a character pointer at declaration time > > > char str[256]; > > > char ch; > > > /* wanna do this but can't cause ch is not a pointer */ > > > strcat(str, ch); > > /* Devious. */ > > sprintf (strchr (str, '\0'), "%c", ch); > Not only devious, but undefined.
Why? There is no overlapping copy, if that's what you're concerned about. -- "It wouldn't be a new C standard if it didn't give a new meaning to the word `static'." --Peter Seebach on C99
|
Fri, 30 Apr 2004 05:10:30 GMT |
|
|