473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Char to char*?

Hi,

How is it possible to convert a char to char* in C? I'm trying to append
a char to the end of a char* using the strcat function.. the following
however is not working:

char* s1, *s2;
char* result;
....

....
result = strcat(s1, (const char*)s2[3]); // application crashes here

What am I doing wrong? thanks

Sona

Nov 13 '05 #1
23 4314
Sona <so**********@nospam.com> writes:
How is it possible to convert a char to char* in C? I'm trying to
append a char to the end of a char* using the strcat function.. the
following however is not working:

char* s1, *s2;
char* result;
...

...
result = strcat(s1, (const char*)s2[3]); // application crashes here


The second argument of strcat() is a const char *. You are
passing a character. The cast to const char * is not going to
help.

You will have to construct a real string:

char tempstr[2];

tempstr[0] = s2[3];
tempstr[1] = '\0';
strcat(s1, tempstr);
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Nov 13 '05 #2
Sona wrote:
Hi,

How is it possible to convert a char to char* in C? I'm trying to append
a char to the end of a char* using the strcat function.. the following
however is not working:

char* s1, *s2;
char* result;
...

...
result = strcat(s1, (const char*)s2[3]); // application crashes here

What am I doing wrong? thanks


Being confused; no shame is attached to that. Try this, which seems to be
what you are trying:

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

#define STRsize 256

int main(void)
{
char base[STRsize] = "base+";
char other[STRsize] = "xxxayyy";
/* appending a character from a string */
strncat(base, other + 3, 1);
printf("%s\n", base);
return 0;
}

[output]
base+a


--
Martin Ambuhl

Nov 13 '05 #3
Ben Pfaff wrote:

Sona <so**********@nospam.com> writes:

...
result = strcat(s1, (const char*)s2[3]); // application crashes here


The second argument of strcat() is a const char *. You are
passing a character. The cast to const char * is not going to
help.

You will have to construct a real string:

Oh, you don't have to create a temp.
size_t len;

len = strlen (s1);

s1[len] = s2[3];

s1[len+1] = 0;

As long as there is room to expand, of course.


Brian Rodenborn
Nov 13 '05 #4
Default User <fi********@company.com> writes:
Ben Pfaff wrote:

Sona <so**********@nospam.com> writes:

...
result = strcat(s1, (const char*)s2[3]); // application crashes here


The second argument of strcat() is a const char *. You are
passing a character. The cast to const char * is not going to
help.

You will have to construct a real string:

Oh, you don't have to create a temp.


Presumably the OP wanted to use strcat().
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #5
Ben Pfaff <bl*@cs.stanford.edu> writes:
Default User <fi********@company.com> writes:
Ben Pfaff wrote:

Sona <so**********@nospam.com> writes:

...
> result = strcat(s1, (const char*)s2[3]); // application crashes here

The second argument of strcat() is a const char *. You are
passing a character. The cast to const char * is not going to
help.

You will have to construct a real string:

Oh, you don't have to create a temp.


Presumably the OP wanted to use strcat().


If not, here's an "easy" way:
sprintf(strchr(s1, '\0'), "%c", s2[3]);
--
"...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
in the uncertainty principle. Which of course makes the above equivalent to
Schrodinger's pointer..."
--Anthony McDonald
Nov 13 '05 #6
Sona wrote:
Hi,

How is it possible to convert a char to char* in C? I'm trying to append
a char to the end of a char* using the strcat function.. the following
however is not working:

char* s1, *s2;
char* result;
...

...
result = strcat(s1, (const char*)s2[3]); // application crashes here

What am I doing wrong? thanks

Sona


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

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

char str3[1];

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

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

return 0;
}

--Steve

Nov 13 '05 #7
Steve Zimmerman <st******@sonic.net> wrote:
<SNIP>
#include <stdio.h>
#include <string.h>

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

char str3[1];

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


BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM - - -
dangelangelangdingplong...

You know what's caused the UB, don't you?

<SNIP>

--
Computer: a million morons working at the speed of light.
Nov 13 '05 #8
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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #9
Tom Zych wrote:

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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13


#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;
}

--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #10
Joe Wright wrote:
strncat(line, &c, 1);


Sweet hack!

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #11
Tom Zych <tz******@pobox.com> wrote:
Joe Wright wrote:
strncat(line, &c, 1);


Sweet hack!


Bitter-sweet, IMHO...
Irrwahn
--
What does this red button do?
Nov 13 '05 #12
Tom Zych wrote:

Joe Wright wrote:
strncat(line, &c, 1);


Sweet hack!

Glad you liked it.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #13
Irrwahn Grausewitz wrote:
Tom Zych <tz******@pobox.com> wrote:
Sweet hack!
Bitter-sweet, IMHO...


?

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #14
Tom Zych <tz******@pobox.com> wrote:
Irrwahn Grausewitz wrote:
Tom Zych <tz******@pobox.com> wrote:
>Sweet hack!

Bitter-sweet, IMHO...


?


Well, if I would encounter a similar construct in production
code I would be in serious need of vacation afterwards (in fact,
this is the case anyway, but that's not the point :-) ).

To be serious: at least I do not consider it to be particular
good style to feed a pointer to a single character to /any/
of C's string processing functions. I'm not even sure if
something like

char c = 's';
strncat( line, &c, 1 );

will not invoke undefined behaviour. Why? Well, AFAIK the
standard does not impose any restrictions on how strncat()
has to handle its second argument. If, just for example,
an implementation choses to perform a strlen() on the second
argument, for whatever reason or purpose, you are doomed.

A much better choice would be memcpy(), IMHO.

Irrwahn
--
What does this red button do?
Nov 13 '05 #15
Irrwahn Grausewitz <ir*****@freenet.de> writes:
I'm not even sure if something like

char c = 's';
strncat( line, &c, 1 );

will not invoke undefined behaviour. Why? Well, AFAIK the
standard does not impose any restrictions on how strncat()
has to handle its second argument. If, just for example,
an implementation choses to perform a strlen() on the second
argument, for whatever reason or purpose, you are doomed.


The standard explicitly refers to the second argument as a pointer to an
array, and not a pointer to a string as in the case of the first argument.
It is therefore my understanding that the second argument is not required
to be a pointer to a string.

Martin
Nov 13 '05 #16
Martin Dickopp <ex*************@zero-based.org> wrote:
Irrwahn Grausewitz <ir*****@freenet.de> writes:
I'm not even sure if something like

char c = 's';
strncat( line, &c, 1 );

will not invoke undefined behaviour. Why? Well, AFAIK the
standard does not impose any restrictions on how strncat()
has to handle its second argument. If, just for example,
an implementation choses to perform a strlen() on the second
argument, for whatever reason or purpose, you are doomed.


The standard explicitly refers to the second argument as a pointer to an
array, and not a pointer to a string as in the case of the first argument.
It is therefore my understanding that the second argument is not required
to be a pointer to a string.


Now, that's interesting. I have still so much to learn about C,
I'll need another two or three lifetimes to get a grip on it.
Sigh...

--
What does this red button do?
Nov 13 '05 #17
Irrwahn Grausewitz <ir*****@freenet.de> wrote in message news:<jq********************************@4ax.com>. ..

<snipped>
of C's string processing functions. I'm not even sure if
something like

char c = 's';
strncat( line, &c, 1 );

will not invoke undefined behaviour.
it wont.
Why? Well, AFAIK the
standard does not impose any restrictions on how strncat()
has to handle its second argument. If, just for example,
an implementation choses to perform a strlen() on the second
argument, for whatever reason or purpose, you are doomed.
but the standard *does* say that the second argument is an array,
not necessarily a c-string. therefore the implementation has not
got any right to be using the second argument as a string.

A much better choice would be memcpy(), IMHO.


for a single char ? just assign poke the value into the string.

line_length = strlen (line);
line [line_length] = c;
line [line_length +1] = '\0';

its more code (2 extra lines) but is relatively clear.
goose,
hand
Nov 13 '05 #18
Joe Wright wrote:

Tom Zych wrote:

Joe Wright wrote:
strncat(line, &c, 1);


Sweet hack!

Glad you liked it.


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


Brian Rodenborn
Nov 13 '05 #19
Ben Pfaff wrote:

Default User <fi********@company.com> writes:

Oh, you don't have to create a temp.


Presumably the OP wanted to use strcat().

Well, as it is obvious that the OP didn't know what to do, it's best to
try to show the correct way not the one closest in-line with the failed
attempt.


Brian Rodenborn
Nov 13 '05 #20
Default User wrote:
> strncat(line, &c, 1);
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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #21
Tom Zych wrote:

Default User wrote:
> > strncat(line, &c, 1);

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.


Perhaps so.


Brian Rodenborn
Nov 13 '05 #22
On Wed, 10 Sep 2003 15:13:46 GMT, Joe Wright
<jo********@earthlink.net> wrote in comp.lang.c:
Tom Zych wrote:

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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13


#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
Nov 13 '05 #23
On Fri, 12 Sep 2003 02:04:15 GMT, Jack Klein <ja*******@spamcop.net>
wrote in comp.lang.c:
On Wed, 10 Sep 2003 15:13:46 GMT, Joe Wright
<jo********@earthlink.net> wrote in comp.lang.c:
Tom Zych wrote:

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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13


#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
Nov 13 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.