c - 打印垃圾值,c

标签 c arrays pointers

我遇到了问题,但不知道原因。 在 main 中,我创建了 2 个字符串数组。 该函数接受 2 个字符串,并根据以下要求创建一个适当大小的新数组: 例如: 数组 1: aviv , 数组 2: 12 , 新数组:a12v12i12v 新数组的大小必须完全相同! 然后将新数组发送到main并打印出来。 我还打印垃圾值。 我检查了新数组的大小,它的大小是正确的。 我的代码:

 char* CreateString(char* str1, char* str2)
    {
        int length1, length2, length3 = 0;
        int i,j, index_help = 0;
        char *str3 = NULL;
        length1 = strlen(str1);
        length2 = strlen(str2);
        for (i = 0; i < length1; i++) // Check the size of the new array

        {
            length3++;
            if (i == (length1 - 1))
            {
                break;
            }
            for (j = 0; j < length2; j++)
            {
                length3++;
            }
        }
        str3 = (char*)malloc(length3+1  * sizeof(char));
        if (str3 == NULL)
        {
            printf("There is not enough memory space\n");
            return 0;
        }
        for (i = 0; i < length1; i++) //Copying data

        {

            str3[index_help] = str1[i];
            if (i == (length1 - 1))
            {
                break;
            }
            for (j = 0; j < length2; j++) 
            {
                index_help++;
                str3[index_help] = str2[j];
            }
            index_help++;

        }
        return str3;
    }

    int main()
    {
        char *str1 = NULL, *str2 = NULL,*str4=NULL;
        int size1, size2,i;
        printf("enter the size of string number 1:\n");
        scanf("%d", &size1);
        printf("enter the size of string number2 :\n");
        scanf("%d", &size2);
        str1 = (char*)malloc((size1 + 1) * sizeof(char));
        if (str1 == NULL)
        {
            printf("There is not enough memory space\n");
            return 0;
        }
        str2 = (char*)malloc((size2 + 1) * sizeof(char));
        if (str2 == NULL)
        {
            printf("There is not enough memory space\n");
            return 0;
        }
        printf("Enter a value for the first string (the size is:%d):\n", size1);
        scanf("%s", str1);
        printf("Enter a value for the second string (the size is:%d):\n", size2);
        scanf("%s", str2);
        str4 = CreateString(str1, str2);
printf("%s",str4);
        printf("\n");
        return 0;
    }

最佳答案

函数应该这样声明

 char* CreateString( const char* str1, const char* str2);

因为字符串 str1 和字符串 str2 在函数中都没有改变。

您必须在结果字符串后附加一个终止零。

无需使用循环即可更简单地计算结果字符串的长度。

由于函数 strlen 具有返回类型 size_t,因此接受函数调用结果的变量也应声明为具有 size_t 类型code> 而不是 int 类型。

该功能可以按照演示程序中所示的方式实现。

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

char * CreateString(const char *s1, const char *s2)
{
    size_t n1 = strlen(s1);
    size_t n2 = strlen(s2);

    size_t n3 = n1 == 0 ? 0 : n1 + (n1 - 1) * n2;

    char *s3 = ( char * )malloc(n3 + 1);

    if (s3)
    {
        if (n2 == 0)
        {
            strcpy(s3, s1);
        }
        else
        {
            char *p = s3;

            while (*s1)
            {
                *p++ = *s1++;
                if (*s1)
                {
                    strcpy(p, s2);
                    p += n2;
                }
            }

            *p = '\0';
        }
    }

    return s3;
}

int main( void )
{
    char *p = CreateString("aviv", "12");

    printf("\"%s\"\n", p);

    free(p);

    p = CreateString("", "12");

    printf("\"%s\"\n", p);

    free(p);

    p = CreateString("a", "12");

    printf("\"%s\"\n", p);

    free(p);

    p = CreateString("av", "12");

    printf("\"%s\"\n", p);

    free(p);

    p = CreateString("av", "");

    printf("\"%s\"\n", p);

    free(p);

    return 0;
}

程序输出为

"a12v12i12v"
""
"a"
"a12v"
"av"

关于c - 打印垃圾值,c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970505/

相关文章:

c - 格式字符串周围的 `TEXT` 在 "printf"中是什么意思

java - Java 消除数组中的重复数字

python - 将 N 个点放入 M 个相等的箱子中

php - 从今天算起接下来的 6 天?

C:数组元素消失

c++ - 定义集合 C++

c - 从文件中读取随机输入

c - 为什么数组名是指向数组第一个元素的指针?

c++ - std::cout 不打印到控制台

带有字符数组的 C++ & 运算符