Is argv[argc] equal to NULL Pointer [duplicate]

有些话、适合烂在心里 提交于 2019-11-27 23:55:06

问题


This question already has an answer here:

  • argv[argc] ==? 2 answers

I read an article (forgot the URL), which said that argv[argc] is a NULL pointer (contains \0). To check whether if its true I wrote this code, yeah it exist. What I don't understand is, why does the OS include this NULL pointer at argv[argc]. Is it useful for something else also?

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

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

    return 0;
}

回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/16418932/is-argvargc-equal-to-null-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!