Why do we need argc while there is always a null at the end of argv?

前端 未结 4 635
离开以前
离开以前 2020-12-08 03:41

It seems that the argv[argc] is always NULL, so I think we can traverse the argument list without argc. A single while lo

4条回答
  •  既然无缘
    2020-12-08 04:00

    Yes, argv[argc] is guaranteed to be a null pointer. argc is used for convenience.

    Quoting the official explanation from C99 Rationale, note the words redundant check:

    Rationale for International Standard — Programming Languages — C §5.1.2.2.1 Program startup

    The specification of argc and argv as arguments to main recognizes extensive prior practice. argv[argc] is required to be a null pointer to provide a redundant check for the end of the list, also on the basis of common practice.

提交回复
热议问题