c 中的数组副本以某种方式添加 ▄■a 即使调试器甚至不显示它

array copy in c somehow adds ▄■a even tho debugger doesnt even show it

我一直在编写代码来检查给定字符串(任意长度)的给定方面。 但是我遇到了这个问题:

line_size = getline(&buffer, &buffsize, stdin);
        int length = line_size - 1;
        char input[length];
        char input_copy[length];
        printf("buffer: %s",buffer);
        for (int i = 0; i < length; i++) input[i] = buffer[i];
        printf("input: %s", input);

不知何故给了我

buffer: test
input: test▄■a

即使调试器显示:

在这一点上我不知道我做错了什么。 真的希望你们能帮帮我。

编辑: 完整计划:

int main(int argc, char *const argv[]) {
    bool ignore_white, ignore_case, has_options = false;
    char *out_path = NULL;
    char *input_path = NULL;
    int c;


    char *buffer;
    size_t buffsize = 0;
    int line_size = 0;
    buffer = NULL;


    myprog = argv[0];

    while ((c = getopt(argc, argv, "s;i;o:")) != -1) {
        has_options = true;
        switch (c) {
            case 's' :
                ignore_white = true;
                break;
            case 'i' :
                ignore_case = true;
                break;
            case 'o':
                out_path = optarg;
                break;
            default:
                usage();
                break;
        }
    }
    if (optind < argc) input_path = argv[optind]; //input_path[] and optind ++ for more then one inputpath
    if (input_path == NULL) {
        line_size = getline(&buffer, &buffsize, stdin);
        int length = line_size - 1;
        char input[length];
        char input_copy[length];
        printf("buffer: %s",buffer);
        for (int i = 0; i <= length; i++) input[i] = buffer[i];
        printf("input: %s", input);

        free(buffer);
        buffer = NULL;

    }
    return 0;
}

只需添加

input[line_size] = '[=10=]';

在这个循环之后:

for (int i = 0; i <= length; i++) input[i] = buffer[i];

*当然把尺寸从input[length]改成input[line_size]