《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.
…….