C语言 C 字符到字符串(将字符传递给 strcat())
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2347457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C char to string (passing char to strcat())
提问by frx08
my problem is in convert a char to string i have to pass to strcat() a char to append to a string, how can i do? thanks!
我的问题是将字符转换为字符串我必须传递给 strcat() 一个字符以附加到字符串,我该怎么办?谢谢!
#include <stdio.h>
#include <string.h>
char *asd(char* in, char *out){
while(*in){
strcat(out, *in); // <-- err arg 2 makes pointer from integer without a cast
*in++;
}
return out;
}
int main(){
char st[] = "text";
char ok[200];
asd(st, ok);
printf("%s", ok);
return 0;
}
采纳答案by legends2k
Since okis pointing to an uninitialized array of characters, it'll all be garbage values, so where the concatenation (by strcat) will start is unknown. Also strcattakes a C-string (i.e. an array of characters which is terminated by a '\0' character). Giving char a[200] = ""will give you a[0] = '\0', then a[1] to a[199] set to 0.
由于ok指向一个未初始化的字符数组,所以它都是垃圾值,所以连接 (by strcat) 将从哪里开始是未知的。还strcat需要一个 C 字符串(即以 '\0' 字符结尾的字符数组)。给予char a[200] = ""会给你 a[0] = '\0',然后 a[1] 到 a[199] 设置为 0。
Edit:(added the corrected version of the code)
编辑:(添加了代码的更正版本)
#include <stdio.h>
#include <string.h>
char *asd(char* in, char *out)
{
/*
It is incorrect to pass `*in` since it'll give only the character pointed to
by `in`; passing `in` will give the starting address of the array to strcat
*/
strcat(out, in);
return out;
}
int main(){
char st[] = "text";
char ok[200] = "somevalue"; /* 's', 'o', 'm', 'e', 'v', 'a', 'l', 'u', 'e', 'char *asd(char* in, char *out)
{
char *end = out + strlen(out);
do
{
*end++ = *in;
} while(*in++);
return out;
}
' */
asd(st, ok);
printf("%s", ok);
return 0;
}
回答by AndiDog
strcatwill not append single characters. Instead it takes a const char*(a full C-style string) which is appended to the string in the first parameter. So your function should read something like:
strcat不会附加单个字符。相反,它需要一个const char*(完整的 C 样式字符串)附加到第一个参数中的字符串。所以你的函数应该是这样的:
#include <stdio.h>
#include <string.h>
int main(){
char st[] = "text";
char ok[200];
ok[0] = 'char* ctos(char c)
{
char s[2];
sprintf(s, "%c##代码##", c);
return s;
}
'; /* OR
memset(ok, 0, sizeof(ok));
*/
strcat(ok, st);
printf("%s", ok);
return 0;
}
The do-while loop will include the zero-terminator which is necessary at the end of C-style strings. Make sure that your out string is initialized with a zero-terminator at the end or this example will fail.
do-while 循环将包括零终止符,这是 C 样式字符串末尾所必需的。确保您的输出字符串在末尾使用零终止符进行初始化,否则此示例将失败。
And as an aside: Think about what *in++;does. It will increment inand dereference it, which is the very same as in++, so the *is useless.
顺便说一句:想想是什么*in++;。它将增加in和取消引用它,这与 非常相同in++,因此*是无用的。
回答by t0mm13b
To look at your code, I can make a couple of pointers in relation to it, this is not a criticism, take this with a pinch of salt that will enable you to be a better C programmer:
为了查看您的代码,我可以提出一些与它相关的指针,这不是批评,请稍加保留,这将使您成为更好的 C 程序员:
- No function prototype.
- Incorrect usage of pointers
- Dealing with the
strcatfunction is used incorrectly. - Overdoing it - no need for the
asdfunction itself! - Usage of dealing with variables notably
chararray that is not properly initialized.
- 没有函数原型。
- 指针使用不当
- 处理
strcat函数使用不当。 - 过度使用 - 不需要
asd函数本身! - 处理变量的用法,特别
char是未正确初始化的数组。
Hope this helps, Best regards, Tom.
希望这会有所帮助,最好的问候,汤姆。
回答by Joseph
To convert a character to a (null terminated) string you could simply do:
要将字符转换为(空终止)字符串,您可以简单地执行以下操作:
##代码##Working example: http://ideone.com/Cfav3e
工作示例:http: //ideone.com/Cfav3e

