简体   繁体   English

连接两个字符串:C

[英]Concatenating two strings : C

Consider Below code for concatenating two char arrays with a delimiter : 考虑下面的代码,用delimiter将两个char arrays连接起来:

void addStrings(char* str1,char* str2,char del)
{
//str1=str1+str2
int len1=strlen(str1);
int len2=strlen(str2);
int i=0;
//char* temp=(char*) malloc((len1+1)*sizeof(char));
//strcpy(temp,str1);
str1=(char*) realloc(str1,(len1+len2+1)*sizeof(char));
printf("Here--%d\n",strlen(str1));
*(str1+len1)=del; //adding delimiter
for(i=0;i<=len2;i++)
    *(str1+len1+i+1)=*(str2+i);
printf("Concatenated String: %s\n",str1);
i=0;
    while( *(str1+i) != '\0')
    {
            printf("~~%d:%c\n",i,*(str1+i));
        i++;
    }

}

When running this function with addStrings("A","test",'@'); 当使用addStrings("A","test",'@');运行此函数时addStrings("A","test",'@'); ; ; The code crashes as realloc below is gdb output 代码崩溃,因为下面的reallocgdb output

Breakpoint 3, addStrings (str1=0x40212f <_data_start__+303> "A", str2=0x40212a <_data_start__+298> "test",
    del=64 '@') at string.c:34
34      int len1=strlen(str1);
(gdb) s
35      int len2=strlen(str2);
(gdb) s
36      int i=0;
(gdb) s
39      str1=(char*) realloc(str1,(len1+len2+1)*sizeof(char));
(gdb)

Program received signal SIGABRT, Aborted.
0x004012f2 in addStrings (str1=0xc0 <Address 0xc0 out of bounds>,
    str2=0xea60 <Address 0xea60 out of bounds>, del=0 '\000') at string.c:39
39      str1=(char*) realloc(str1,(len1+len2+1)*sizeof(char));

Not able to figure out why it is crashing? 无法弄清楚为什么它崩溃了? Is it because I am passing str1 as auto variable rather than creating it on heap ? 是因为我将str1作为auto变量传递,而不是在heap创建它吗?

If this is the case ? 如果是这样的话? How do I modify my code to accept auto as well as heap variables? 如何修改代码以接受auto变量和heap变量?

You need to pass your target string pointer by address, and it must hold either the address of a previously allocate string, or NULL (if coded correctly). 您需要按地址传递目标字符串指针,并且它必须保存先前分配的字符串的地址或NULL(如果编码正确)。 The size allocation must be both lengths + 2 (one for the deli separator, one for the terminator). 大小分配必须为两个长度+ 2(熟食分隔符一个,终止符一个)。 The result can look something like this: 结果可能如下所示:

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

void addStrings(char** str1, const char* str2,char del)
{
    size_t len1 = *str1 ? strlen(*str1) : 0;
    size_t len2 = str2 ? strlen(str2) : 0;
    char *res = realloc(*str1, len1 + len2 + 2);
    if (res)
    {
        res[len1] = del;
        memcpy(res + len1 + 1, str2, len2);
        res[len1 + 1 + len2] = 0;
        *str1 = res;
    }
}

int main()
{
    char *p = NULL;
    const char test[] = "test";
    int i=0;

    // prove it works with no input whatsoever
    addStrings(&p, NULL, 'X');
    printf("p = %p, %s\n", p, p);

    // loop on some input for awhile
    for (;i<10;++i)
    {
        addStrings(&p, test, '@');
        printf("p = %p, %s\n", p, p);
    }
    free(p);
    return 0;
}

Output 产量

p = 0x128610, X
p = 0x128610, X@test
p = 0x128610, X@test@test
p = 0x128620, X@test@test@test
p = 0x128620, X@test@test@test@test
p = 0x128620, X@test@test@test@test@test
p = 0x128620, X@test@test@test@test@test@test
p = 0x128640, X@test@test@test@test@test@test@test
p = 0x128640, X@test@test@test@test@test@test@test@test
p = 0x128640, X@test@test@test@test@test@test@test@test@test
p = 0x128670, X@test@test@test@test@test@test@test@test@test@test

Compiled with: Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: i386-apple-darwin13.2.0 Thread model: posix 编译:Apple LLVM版本5.1(clang-503.0.40)(基于LLVM 3.4svn)目标:i386-apple-darwin13.2.0线程模型:posix

Note the change in resulting address on some of the passes. 注意某些通行证中结果地址的变化。 I leave the checking for valid parameter input as an exercise for you. 我将检查有效的参数输入留给您作为练习。

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

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