字符串(命令行)如何存储在 char**argv 和 int *argv 中?

how string (command line) are stored in char**argv and int *argv?

第一个片段:

#include<stdio.h>
int main(int argc, char **argv)
{
int i;
for(i=1; i<argc; i++)
    printf("%s\n", argv[i]);
return 0;
}

加载时间输入:

./a.out devang samir

输出:

devang
samir

第二个片段:

#include<stdio.h>
int main(int argc, int *argv)
{
int i;
for(i=1; i<argc; i++)
    printf("%s\n", argv[i]);
return 0;
}

加载时间输入:

./a.out devang samir

输出:

devang
samir

在这两种情况下,我得到的输出相同,但为什么呢?

C11 标准将第 5.1.2.2.1 章中 main() 的函数签名指定为

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;[...]

关于约束,

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings,[...]

那么,在你的第二种情况下,

 int main(int argc, int *argv)

char*int(一般来说 argv[n])是完全不同的类型(即不兼容的类型),您的第二个程序调用 undefined behavior

详细说明,如果函数没有原型,调用时传递给函数的参数应该完全匹配预期参数的类型。

引用标准,章节 §6.5.2.2

[...] If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with those of the parameters after promotion, the behavior is undefined.