This question was migrated from Code Review Stack Exchange because it can be answered on Stack Overflow. Migrated两年前Learn more
我的测试用例在向结果变量写入垃圾时遇到问题。我对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

最佳答案

嵌套函数不是标准C的一部分。只有GCC(可能是Clang仿真,或者与GCC兼容)支持它如果希望在不传递有关嵌套函数不适用性的尖锐注释的情况下离开,请不要在堆栈溢出(或代码检查)时发布嵌套函数。
您的问题是r中的变量main是一个大小1的数组,但是您在remover()函数中使用它,就好像它更大一样结果你会有不明确的行为。
至少,您应该使用:

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

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

07-26 06:03
查看更多