C:主函数中的argv声明
您认为哪种方式在 main 函数中声明 argv 参数更好?为什么?
int main(int argc, char **argv /* char *argv[] */ /* char (*argv)[] */) {
//...
}
argv 最终作为指针进入函数 - 只是作为 argv 地址的副本,但不是作为数组,对吗?所以我认为,其他选择在语法上也必须是正确的,但你更喜欢哪种方式?
问候
What do you think which way is better declaring the argv argument in the main function and why?
int main(int argc, char **argv /* char *argv[] */ /* char (*argv)[] */) {
//...
}
argv comes into the function ultimately as a pointer - just as a copy of the argv address, but not as an array, right? So I think, the other alternatives must also be syntactically correct, but which way would you prefer?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
char *argv[]
的含义最为明确——字符串数组。如果你很懒的话,
char **argv
是最快的输入方式。char (*argv)[]
我不确定你为什么要使用。char *argv[]
is most explicit as to what it means -- an array of strings.char **argv
is fastest to type if you're lazy.char (*argv)[]
I'm not sure why you'd use.使用
*argv[]
是传统方式,但我也见过使用**argv
的程序。变体
(*argv)[]
实际上与*argv[]
相反,第一个是指向数组的指针(在本例中这是错误的),最后是一个指针数组。Using
*argv[]
is the traditional way, but I have seen programs using**argv
as well.The variant
(*argv)[]
is actually the opposite of*argv[]
, the first is a pointer to an array (which is wrong in this case), the last is an array of pointers.我个人更喜欢
,因为它更好地描述了 argv,IMO。它是一个
char*
数组,即一个字符串数组,这正是我所期望的argv
。不是双指针。第三种变体看起来不错,与第二种相同。I personally prefer
because it describes
argv
better, IMO. It's an array ofchar*
, that is an array of strings, which is what I expectargv
to be. Not a double pointer. The third variant seems ok and is equivalent to the second one.我喜欢 char *argv[] ,因为它是实际传递内容的最清晰表达;指向字符串的指针数组。
char **argv
更简洁,但我觉得“指向指针的指针”并不能准确描述正在传递的内容。第三个选项中额外的括号只会让事情变得混乱。
I like
char *argv[]
as it is the clearest expression of what is actually passed; an array of pointers to character strings.char **argv
is more consise but I feel "pointer to pointer" does not accuratly describe wahts being passed.The extra parenthesis in the third option just confuse things.