converting char to pointer char 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 10 post ] 

 Relevant Pages 

1. Converting char array of literals chars to escape chars

2. A char pointer (char *) vs. char array question

3. Comparison with pointer to pointer to char, and char

4. converting an char pointer to an integer pointer

5. Convert char* to char __gc[]

6. cannot convert from char* to char[4]

7. Convert four chars to a long and a long to four chars

8. Converting unsigned char to signed char invokes UB?

9. Convert an 8bit char in a 7bit char!

10. converting const char* to unsigned char[1024]

11. Convert from char to unsigned char

12. convert char to const char

 

 
Powered by phpBB® Forum Software