C字符串变为空

Luc*_*cas 1 c argv command-line-arguments

我正在学习如何在C中获取参数,但是,当我使用以下输入运行下面的代码时,第一个变为null.

输入: ./a.out a b c d e f g h i j k

输出: (null) b c d e f g h i j k

#include <stdio.h>

    int main(int argc, char *argv[])
    {
        int i = 2, j = 0;
        char *foo = argv[1];
        char *bar[10];
        while(j < 10 && i < argc)
        {
            bar[j++] = argv[i++];
        }
        bar[j] = NULL;

        printf("%s ", foo);
        for(j = 0; bar[j] != NULL; j++)
        {
            printf("%s ", bar[j]);
        }
        printf("\n");

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

Dan*_*her 5

在循环结束时,您写NULLbar[10]的,但你只有分配bar[0 - 9].这可能会覆盖foo.