简体   繁体   English

连接两个字符串但得到 null。 请告诉我这个错误

[英]Concatenating two strings but getting null. please pont me error in this

I am trying to copy two strings to a new pointer.我正在尝试将两个字符串复制到一个新指针。 this is to concatenate string pointer with sub pointer.这是将字符串指针与子指针连接起来。 Trying to make the output in new pointer str.what is wrong in this?试图在新指针 str 中创建 output。这有什么问题?

void addsubstring( char* string,  char* sub)
{
    //int m = strlen(string);
    char *str=(char*)malloc(sizeof(char)*10);
    int n =0;
    while( *string != '\0')
    {
        *(str) = *(string++);
        printf("char is %c\n", str[n++]);
        str++;
    }
        while( *sub != '\0')
    {
        *(str) = *(sub++);
        printf("char is %c\n", str[n++]);
        str++;``
    }
    printf("%s", str);
}

For starters this memory allocation using the magic number 10 makes the function unsafe.对于初学者来说,这个 memory 分配使用幻数10使 function 不安全。

char *str=(char*)malloc(sizeof(char)*10);

In these while loops在这些 while 循环中

while( *string != '\0')
{
    *(str) = *(string++);
    printf("char is %c\n", str[n++]);
    str++;
}
    while( *sub != '\0')
{
    *(str) = *(sub++);
    printf("char is %c\n", str[n++]);
    str++;``
}

the pointer str is being changed (incremented).指针str正在更改(递增)。 So after the loops it points to a garbage.所以在循环之后它指向一个垃圾。

And you forgot to include in the result string the terminating zero character '\0' .而且您忘记在结果字符串中包含终止零字符'\0'

The function can be defined the following way function 可以通过以下方式定义

char * addsubstring( const char *s1,  const char *s2 )
{
    size_t n1 = strlen( s1 );
    char *result = malloc( n1 + strlen( s2 ) + 1 );

    if ( result != NULL )
    {
        memcpy( result, s1, n1 );
        strcpy( result + n1, s2 );
    }

    return result;
}

Here is a demonstrative program这是一个演示程序

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

char * addsubstring( const char *s1,  const char *s2 )
{
    size_t n1 = strlen( s1 );
    char *result = malloc( n1 + strlen( s2 ) + 1 );

    if ( result != NULL )
    {
        memcpy( result, s1, n1 );
        strcpy( result + n1, s2 );
    }

    return result;
}

int main(void) 
{
    char *p = addsubstring( "Hello ", "World!" );
    
    if ( p ) puts( p );
    
    free( p );
    
    return 0;
}

The program output is程序 output 是

Hello World!

If you may not use standard C string functions then the function can be defined as it is shown in the demonstrative program below如果您可能不使用标准的 C 字符串函数,则可以定义 function,如下面的演示程序所示

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

char * addsubstring( const char *s1,  const char *s2 )
{
    size_t n = 0;
    
    while ( s1[n] ) ++n;
    while ( s2[n] ) ++n;
    
    char *result = malloc( n + 1 );

    if ( result != NULL )
    {
        char *p = result;
        while ( *s1 ) *p++ = *s1++;
        
        while ( ( *p++ = *s2++ ) );
    }

    return result;
}

int main(void) 
{
    char *p = addsubstring( "Hello ", "World!" );
    
    if ( p ) puts( p );
    
    free( p );
    
    return 0;
}

The program output is the same as shown above程序output同上图

Hello World!

The problem is the pointer str is slided.问题是指针str滑动了。 You should create another pointer to use as the origin.您应该创建另一个指针用作原点。 Also you will have to terminate the stirng by adding null-character before passing that for %s .此外,您还必须通过添加空字符来终止搅拌,然后再将其传递给%s

Also don't forget to free the allocated buffer, or a memory leak occurs.也不要忘记释放分配的缓冲区,否则会发生 memory 泄漏。

Another point is that casting results of malloc() family is considered as a bad practice .另一点是malloc()系列的强制转换结果被认为是一种不好的做法

Fixed code:固定代码:

void addsubstring( char* string,  char* sub)
{
    //int m = strlen(string);
    char *str=malloc(sizeof(char)*10);
    char *str_start = str;
    int n =0;
    while( *string != '\0')
    {
        *(str) = *(string++);
        printf("char is %c\n", str_start[n++]);
        str++;
    }
        while( *sub != '\0')
    {
        *(str) = *(sub++);
        printf("char is %c\n", str_start[n++]);
        str++;
    }
    *(str) = '\0';
    printf("%s", str_start);
    free(str_start);
}

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

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