Char to char*?

T

Tom Zych

Using a function call of any sort where a simple array insert takes care
of the problem seems somewhat overdone.

The array insert wouldn't be so simple. You have to use strlen to
find the right place. Then you have to copy c. Then you have to
terminate line again. This one call does all that.
 
D

Default User

Tom said:
The array insert wouldn't be so simple. You have to use strlen to
find the right place. Then you have to copy c. Then you have to
terminate line again. This one call does all that.

Perhaps so.




Brian Rodenborn
 
J

Jack Klein

Tom said:
Steve said:
int main()
{
char str1[] = "Your grade is ";
char str2[] = "A";

char str3[1];

strcpy(str3, strcat(str1, str2));

printf("%s\n", str3);

return 0;
}

strcat changes dest in place. str1 can't be changed. str3 is only
one byte long.

#include <stdio.h>
#include <string.h>

int main(void) {
char line[100];
char *s1 = "Happy Day";
char c = 's';
strcpy(line,s1);
strncat(line, &c, 1);
printf("%s\n", line);
return 0;
}

Undefined behavior.

--
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
 
J

Jack Klein

Tom said:
Steve Zimmerman wrote:

int main()
{
char str1[] = "Your grade is ";
char str2[] = "A";

char str3[1];

strcpy(str3, strcat(str1, str2));

printf("%s\n", str3);

return 0;
}

strcat changes dest in place. str1 can't be changed. str3 is only
one byte long.

#include <stdio.h>
#include <string.h>

int main(void) {
char line[100];
char *s1 = "Happy Day";
char c = 's';
strcpy(line,s1);
strncat(line, &c, 1);
printf("%s\n", line);
return 0;
}

Undefined behavior.

Sorry, my bad, no it's not.

--
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
 
P

Peter Shaggy Haywood

Groovy hepcat Sona was jivin' on Wed, 10 Sep 2003 06:44:29 +1000 in
comp.lang.c.
Char to char*?'s a cool scene! Dig it!
How is it possible to convert a char to char* in C? I'm trying to append

Don't. There's a better way.
a char to the end of a char* using the strcat function.. the following

Try this:

char *charcat(char *s, char c)
{
size_t len = strlen(s);

s[len] = c;
s[len + 1] = '\0';

return s;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top