【问题标题】:Why am I getting garbage characters in my output for Test 1 and Test3 occasionally?为什么我偶尔会在测试 1 和测试 3 的输出中出现乱码?
【发布时间】:2017-01-17 01:43:22
【问题描述】:

我的测试用例将垃圾写入结果变量时遇到问题。我对 C 很陌生,所以我无法确定是什么原因造成的。

//Author: Ryan Fehr
//Contributors:
#include <stdio.h>
#include <string.h>

int remover(char[], char[], char[]);

int remover(char source[], char substring[], char result[])
{
    char *current = source;
    // printf("%s n", current);
    char *currentSub = substring;
    //printf("%c n", *currentSub);
    int i = 0;

    while(*current != '\0')//Loops through the source string
    {
        //Uncommenting the line below will show you the comparisons being performed
        printf(" %c | %c \n", *current, *currentSub);

        if(*current == *currentSub || *currentSub == '\0')//True when a letter matches with a letter in the subStr or the complete subStr was found
        {

            if(*currentSub == '\0')
            {
                char pre[((current-(i) - source))];//Stores everything before the subString in pre(current-i) - source
                memcpy(pre, source, (current-i) - source);
                printf("Pre: %s\n",pre);
                //Counts how many chars are after the substring
                int n = 0;
                while(*current != '\0')
                {
                    n++;
                    current++;
                }
                char post[n];//Stores everything after the subString in post
                memcpy(post, current-n, n);
                printf("Post: %s\n",post);
                strcat(result, pre);
                strcat(result,post);
                printf("Substring removed: %s\n", result);//Prints the value after substring has been removed
                return 1;
            }
            i++;
            currentSub++;
        }
        else
        {
            i=0;
            currentSub = substring;
        }
        current++;
    }
    return 0;
}

int main(void)
{
    //TEST 1
    char s[] = "jump_on_down_to_getfart_and_up_to_get_down_";
    char sub[] = "fart";
    char r[100] = "";
    printf("Test 1:\n");
    printf("Source: %snSubstring: %s\n",s,sub);
    printf("%d\n\n", remover(s, sub, r));
    //EXPECTED OUTPUT: 1
    //TEST 2
    strcpy(s, "racecar");
    strcpy(sub, "x");
    strcpy(r, "");
    printf("Test 2:n");
    printf("Source: %snSubstring: %s\n",s,sub);
    printf("%d\n\n", remover(s, sub, r));
    //EXPECTED OUTPUT: 0
    //TEST 3
    strcpy(s, "jump on down to get and up to get down ");
    strcpy(sub, "up");
    strcpy(r, "");
    printf("Test 3:n");
    printf("Source: %snSubstring: %s\n",s,sub);
    printf("%d\n\n", remover(s, sub, r));
    //EXPECTED OUTPUT: 1
}

这是 Test1 输出的屏幕截图,如您所见,我得到了额外的垃圾打印,我认为我的数学对我的子字符串是正确的,所以我不确定是什么原因造成的。

I can't embed images so it is linked

【问题讨论】:

  • 1) strcat(result, pre); : result 没有足够的空间。
  • 在标准 C 中,不允许在其他函数中包含函数定义;将这些定义移出main 是个好主意

标签: c memory-management pointers


【解决方案1】:

嵌套函数不是标准 C 的一部分。只有 GCC(可能还有 Clang 模拟,或与 GCC 的兼容模式)支持它。如果您想避免因嵌套函数的不适用而受到严厉批评,请不要在 Stack Overflow(或 Code Review)上发布嵌套函数。

您的问题是main 中的变量r 是一个大小为1 的数组,但您在remover() 函数中使用它时就好像它更大一样。因此,您会得到未定义的行为。

至少,您应该使用:

char r[100];  // Or any other convenient size - for the test data 50 would do

可能还有其他问题;我还没有编译代码(我拒绝编译带有嵌套函数的 C 代码;它不会通过我的默认编译选项)。

【讨论】:

    【解决方案2】:

    不要在另一个函数中插入一个函数。

    发布的代码在main() 函数中嵌套了remover() 函数。

    虽然一些编译器允许将其作为“扩展”,(想到gcc)你不应该嵌套函数。

    为了便于阅读和理解:1) 一致地缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '} 之前不缩进。建议每个缩进级别使用 4 个空格。 2) 通过空行分隔代码块(for、if、else、while、do...while、switch、case、default)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 2013-03-03
      相关资源
      最近更新 更多