11

我读了一篇文章(忘记了网址),上面说argv[argc]是一个NULL指针(包含\0)。为了检查它是否是真的,我写了这段代码,是的,它存在。我不明白的是,为什么操作系统NULLargv[argc]. 它对其他东西也有用吗?

int
main (int argc, char **argv){

    while (*argv)
        printf ("%s\n", *argv++);

    return 0;
}
4

2 回答 2

11

The C Standard 5.1.2.2.1/2 second mark says explicitly

argv[argc] shall be a null pointer.

The C++ Standard 3.6.1/2 also says explicitly

The value of argv[argc] shall be 0.

于 2013-05-07T12:26:04.653 回答
5

The Standard (C99 5.1.2.2.1p2) mandates that:

If they are declared, the parameters to the main function shall obey the following constraints:

— The value of argc shall be nonnegative.

— argv[argc] shall be a null pointer.

...

The rationale for this is to provide a redundant check for the end of the argument list, on the basis of common practice (ref: Rationale for the ANSI C programming language (1990), 2.1.2.2).

于 2013-05-07T12:26:27.483 回答