为什么我偶尔在测试1和测试3的输出中得到垃圾字符?

时间:2017-01-17 01:43:22

标签: c memory-management pointers

我的测试用例将垃圾写入结果变量时出现问题。我对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

2 个答案:

答案 0 :(得分:4)

嵌套函数不是标准C的一部分。只有GCC(可能是Clang仿真,或者与GCC兼容模式)支持它。如果您希望在不对嵌套函数不适合传递严厉评论的情况下离开,请不要在Stack Overflow(或Code Review)上发布嵌套函数。

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

至少应该使用:

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

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

答案 1 :(得分:2)

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

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

虽然有些编译器允许将其作为&#39;扩展&#39;,(gcc会想到),但您永远不应该嵌套函数。

为了便于阅读和理解:1)始终缩进代码。在每个开口支架后缩进&#39; {&#39;。在每个闭幕式之前都是无意义的。建议每个缩进级别使用4个空格。 2)通过空白行分隔代码块(对于if,else,while,do ... while,switch,case,default)