Char to char *? [英] Char to char*?
问题描述
如何在C中将char转换为char *?我正在尝试使用strcat函数将
a char附加到char *的末尾..以下
但是不起作用:
char * s1,* s2;
char * result;
....
。 ...
result = strcat(s1,(const char *)s2 [3]); //应用程序在这里崩溃
我做错了什么?谢谢
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
推荐答案
Sona< so ******* ***@nospam.com>写道:
Sona <so**********@nospam.com> writes:
如何在C中将char转换为char *?我正在尝试使用strcat函数将char添加到char *的末尾..
以下不起作用:
char * s1,* s2;
char * result;
...
...
result = strcat(s1,(const char *)s2 [3]); //应用程序在这里崩溃
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
strcat()的第二个参数是一个const char *。你传递一个角色是
。对const char *的强制转换不是
帮助。
你必须构建一个真正的字符串:
char tempstr [2];
tempstr [0] = s2 [3];
tempstr [1] =''\'0' ';
strcat(s1,tempstr);
-
C有它的问题,但是从头开始设计的语言会有还有一些,
我们知道C'的问题。
- Bjarne Stroustrup
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
Sona写道:
Sona wrote:
如何在C中将char转换为char *?我正在尝试使用strcat函数将char添加到char *的末尾..以下
然而不起作用:
char * s1,* s2;
char * result;
...
...
result = strcat(s1,(const char *)s2 [3]); //应用程序在这里崩溃
我做错了什么?谢谢
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
困惑;没有羞耻。尝试这个,这似乎是你想要的东西:
#include< stdio.h>
#include < string.h>
#define STRsize 256
int main(无效)
{
char base [STRsize] =" base +" ;;
char其他[STRsize] =" xxxayyy" ;;
/ *附加一个字符串中的字符* /
strncat(base,other + 3,1);
printf("%s \ n",base);
返回0;
}
[输出]
base + a
>
-
Martin Ambuhl
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
Ben Pfaff写道:
Ben Pfaff wrote:
Sona< so ********** @ nospam.com>写道:
Sona <so**********@nospam.com> writes:
...
...
result = strcat(s1,(const char *)s2 [3]); //应用程序在这里崩溃
result = strcat(s1, (const char*)s2[3]); // application crashes here
strcat()的第二个参数是一个const char *。你正在传递一个角色。对const char *的强制转换不会给你提供帮助。
你必须构建一个真正的字符串:
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:
哦,你不要不得不创造一个温度。
size_t len;
len = strlen(s1);
s1 [len] = s2 [3];
s1 [len + 1] = 0;
只要有空间当然,扩展。
Brian Rodenborn
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
这篇关于Char to char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!