c 为什么*argv可以与NULL进行比较?

C语言 码拜 9年前 (2015-10-23) 868次浏览
《C和指针》上的一个例子:

// 打印命令行参数
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
        while(*++argv != NULL)
                printf("%s\n", *argv);
        return EXIT_SUCCESS;
}

argv是指向字符指针的指针,或说字符串数组的指针,但是这个指针数组的最后一个并非NULL,而且越界的第一个值可能是随机的,不一定是NULL,为什么这里能够用*argv与NULL比较来判断能否参数列表已经读取完了呢?

解决方案:20分
5.1.2.2.1 Program startup

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

…….


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c 为什么*argv可以与NULL进行比较?
喜欢 (0)
[1034331897@qq.com]
分享 (0)