简体   繁体   English

用strcpy连接字符串

[英]Concatenate string with strcpy

Here is my code 这是我的代码

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


int main() {

 char f[] = "First";
 char s[] = "Second";
 char *tmp = malloc(strlen(f) + strlen(s) + 2);
 strcpy(tmp, f);
 strcpy(tmp, s);
 printf("%s", tmp);
 free(tmp);
 return 0;
}

I'm trying to concatenate f and s. 我正在尝试连接f和s。 The problem is that tmp contains only "Second" as a array. 问题是tmp只包含“Second”作为数组。 What I miss here 我想念的是什么

strcpy将字符串复制到目标的开头,您需要strcat

The second strcpy overwrites the previous one. 第二个strcpy覆盖前一个。 Both copy its content to the tmp pointer (at the start of it). 两者都将其内容复制到tmp指针(在它的开头)。 You should use tmp+strlen(f) . 你应该使用tmp+strlen(f)

Or even better use strcat . 或者甚至更好地使用strcat

And even better use more secure methods like: strncpy , strncat , etc.. 甚至更好地使用更安全的方法,如: strncpystrncat等。

If you insist on using strcpy , your code should be slightly modified: 如果你坚持使用strcpy ,你的代码应该稍加修改:

int main() {
    const char *f = "First";
    const char *s = "Second";
    char *tmp = malloc(strlen(f) + strlen(s) + 1);
    strcpy(tmp, f);
    strcpy(tmp+strlen(f), s);
    printf("%s", tmp);
    free(tmp);
    return 0;
}

You should consider using strncpy instead of strcpy for safety reasons. 出于安全原因,您应该考虑使用strncpy而不是strcpy Also, strcat is a more conventional function for concatenating C string. 此外, strcat是用于连接C字符串的更常规功能。

EDIT Here is an example of using strncpy instead of strcpy 编辑以下是使用strncpy而不是strcpy的示例

#define MAX 1024

int main() {
    const char *f = "First";
    const char *s = "Second";
    size_t len_f = min(strlen(f), MAX);
    size_t len_s = min(strlen(s), MAX);
    size_t len_total = len_f + len_s;
    char *tmp = malloc(len_total + 1);
    strncpy(tmp, f, len_f);
    strncpy(tmp+len_f, s, len_s);
    tmp[len_total] = '\0';
    printf("%s", tmp);
    free(tmp);
    return 0;
}

You may want to use strcat instead of your second strcpy call, like this: 您可能希望使用strcat而不是第二个strcpy调用,如下所示:

strcpy(tmp, f);
strcat(tmp, s);

Note also that allocating strlen(f) + strlen(s) + 1 bytes for tmp is sufficient no need to allocate strlen(f) + strlen(s) + 2 bytes. 另请注意,为tmp分配strlen(f) + strlen(s) + 1个字节就足够了,不需要分配strlen(f) + strlen(s) + 2字节。 After concatenation, you'll get only one string, so only one null character is required. 连接后,您将只获得一个字符串,因此只需要一个空字符。

using strcat() instead, which means append a string accroding to the MSDN doc.strcpy() just means copy a string. 使用strcat()代替,这意味着附加一个字符串,加入MSDN doc.strcpy()只意味着复制一个字符串。 If you don't want to use strcat(), you should point out the position by using strncpy() or strcpy_s(). 如果您不想使用strcat(),则应使用strncpy()或strcpy_s()指出位置。 Please refer to the document. 请参阅文件。

The problem is that you copy the second string in place of the first one (the first parameter of strcpy() is where to copy the string) and this effectively overwrites the first string. 问题是你复制第二个字符串代替第一个字符串( strcpy()的第一个参数是复制字符串的位置),这有效地覆盖了第一个字符串。 Here's an idea of what you need: 以下是您需要的概念:

size_t firstLen = strlen( f );
size_t secondLen = strlen( s );    
char *tmp = malloc(firstLen + secondLen + 1);
strcpy(tmp, f);
strcpy(tmp + firstLen, s);

This can be achieved by using strcat() , although that would lead to an extra scan along the copied string. 这可以通过使用strcat()来实现,尽管这会导致沿复制的字符串进行额外的扫描。

Here is the correct idiomatic safe way to do what you want: 这是正确的惯用安全方式来做你想要的:

size_t l = strlen(f);
char *tmp = malloc(l + strlen(s) + 1);
strcpy(tmp, f);
strcpy(tmp+l, s);

or: 要么:

size_t l = strlen(f) + strlen(s) + 1;
char *tmp = malloc(l);
snprintf(tmp, l, "%s%s", f, s);

I tend to prefer the latter unless you're writing embedded systems code where you want to avoid pulling in printf dependency. 我倾向于选择后者,除非您编写嵌入式系统代码,以避免拉入printf依赖。

Finally, note that you should be testing malloc for failure, and that it's useless and harmful to allocate memory and copy the strings if all you want to do is print them - you could just do the following: 最后,请注意,您应该测试malloc是否失败,如果要打印它们,分配内存并复制字符串是无用且有害的 - 您可以执行以下操作:

printf("%s%s", f, s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM