C语言 在 C 程序中连接两个 char* 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18468229/
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
Concatenate two char* strings in a C program
提问by BeCurious
I wrote the following C program:
我编写了以下 C 程序:
int main(int argc, char** argv) {
char* str1;
char* str2;
str1 = "sssss";
str2 = "kkkk";
printf("%s", strcat(str1, str2));
return (EXIT_SUCCESS);
}
I want to concatenate the two strings, but it doesn't work.
我想连接两个字符串,但它不起作用。
回答by dcaswell
The way it works is to:
它的工作方式是:
- Malloc memory large enough to hold copies of str1 and str2
- Then it copies str1 into str3
- Then it appends str2 onto the end of str3
- When you're using str3 you'd normally free it
free (str3);
- Malloc 内存大到足以容纳 str1 和 str2 的副本
- 然后将 str1 复制到 str3
- 然后将 str2 附加到 str3 的末尾
- 当您使用 str3 时,您通常会释放它
free (str3);
Here's an example for you play with. It's very simple and has no hard-coded lengths. You can try it here: http://ideone.com/d3g1xs
这是一个供您玩耍的示例。它非常简单,没有硬编码长度。你可以在这里尝试:http: //ideone.com/d3g1xs
See this postfor information about size of char
有关字符大小的信息,请参阅此帖子
#include <stdio.h>
#include <memory.h>
int main(int argc, char** argv) {
char* str1;
char* str2;
str1 = "sssss";
str2 = "kkkk";
char * str3 = (char *) malloc(1 + strlen(str1)+ strlen(str2) );
strcpy(str3, str1);
strcat(str3, str2);
printf("%s", str3);
return 0;
}
回答by kol
Hereis a working solution:
这是一个有效的解决方案:
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
char str1[16];
char str2[16];
strcpy(str1, "sssss");
strcpy(str2, "kkkk");
strcat(str1, str2);
printf("%s", str1);
return 0;
}
Output:
输出:
ssssskkkk
You have to allocate memory for your strings. In the above code, I declare str1and str2as character arrays containing 16 characters. I used strcpyto copy characters of string literals into them, and strcatto append the characters of str2to the end of str1. Here is how these character arrays look like during the execution of the program:
您必须为字符串分配内存。在上面的代码中,我将str1和声明str2为包含 16 个字符的字符数组。我曾经strcpy将字符串文字的字符复制到它们中,strcat并将 的字符附加str2到str1. 下面是这些字符数组在程序执行过程中的样子:
After declaration (both are empty):
str1: [][][][][][][][][][][][][][][][][][][][]
str2: [][][][][][][][][][][][][][][][][][][][]
After calling strcpy (char destination[10]; // 5 times s, 4 times k, one zero-terminator
char* str1;
char* str2;
str1 = "sssss";
str2 = "kkkk";
strcpy(destination, str1);
printf("%s",strcat(destination,str2));
is the string terminator zero byte):
str1: [s][s][s][s][s][char str1[1024];
char *str2 = "kkkk";
strcpy(str1, "ssssss");
strcat(str1, str2);
printf("%s", str1);
][][][][][][][][][][][][][][]
str2: [k][k][k][k][printf("%s%s",str1,str2);
][][][][][][][][][][][][][][][]
After calling strcat:
str1: [s][s][s][s][s][k][k][k][k][char *str1 = calloc(sizeof("SSSS")+sizeof("KKKK")+1,sizeof *str1);
strcpy(str1,"SSSS");
strcat(str1,str2);
][][][][][][][][][][]
str2: [k][k][k][k][##代码##][][][][][][][][][][][][][][][]
回答by Sam I am says Reinstate Monica
strcatconcats str2onto str1
strcat连接str2到str1
You'll get runtime errors because str1is not being properly allocated for concatenation
您将收到运行时错误,因为str1未正确分配用于连接
回答by rmhartog
When you use string literals, such as "this is a string"and in your case "sssss"and "kkkk", the compiler puts them in read-only memory. However, strcatattempts to write the second argument after the first. You can solve this problem by making a sufficiently sized destination buffer and write to that.
当您使用字符串文字时,例如"this is a string"和 在您的情况下"sssss"和"kkkk",编译器将它们放在只读内存中。但是,strcat尝试在第一个参数之后编写第二个参数。您可以通过制作足够大的目标缓冲区并写入该缓冲区来解决此问题。
Note that in recent compilers, you usually get a warning for casting string literals to non-const character pointers.
请注意,在最近的编译器中,您通常会收到将字符串文字转换为非常量字符指针的警告。
回答by TieDad
strcat(str1, str2)appends str2 after str1. It requires str1 to have enough space to hold str2. In you code, str1 and str2 are all string constants, so it should not work. You may try this way:
strcat(str1, str2)在 str1 后附加 str2。它需要 str1 有足够的空间来容纳 str2。在你的代码中, str1 和 str2 都是字符串常量,所以它不应该工作。你可以试试这个方法:
回答by Oliver Matthews
strcatattempts to append the second parameter to the first. This won't work since you are assigning implicitly sized constant strings.
strcat尝试将第二个参数附加到第一个参数。这将不起作用,因为您正在分配隐式大小的常量字符串。
If all you want to do is print two strings out
如果您只想打印出两个字符串
##代码##Would do.
会做。
You could do something like
你可以做类似的事情
##代码##to create a concatenated string; however strongly consider using strncat/strncpy instead. And read the man pages carefully for the above. (oh and don't forget to freestr1 at the end).
创建一个连接的字符串;但是强烈考虑使用 strncat/strncpy 代替。并仔细阅读上述手册页。(哦,free最后别忘了str1 )。

