Char to char*?

S

Sona

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
 
B

Ben Pfaff

Sona said:
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);
 
M

Martin Ambuhl

Sona said:
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
 
D

Default User

Ben said:
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
 
B

Ben Pfaff

Default User said:
Ben said:
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().
 
B

Ben Pfaff

Ben Pfaff said:
Default User said:
Ben said:
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]);
 
S

Steve Zimmerman

Sona said:
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
 
I

Irrwahn Grausewitz

Steve Zimmerman said:
#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>
 
T

Tom Zych

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

Joe Wright

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

Irrwahn Grausewitz

Tom Zych said:

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
 
M

Martin Dickopp

Irrwahn Grausewitz said:
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
 
I

Irrwahn Grausewitz

Martin Dickopp said:
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...
 
G

goose

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
 
D

Default User

Joe said:
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
 
D

Default User

Ben said:
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
 

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