C:主函数中的argv声明

发布于 2024-12-15 02:34:25 字数 251 浏览 5 评论 0原文

您认为哪种方式在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

束缚m 2024-12-22 02:34:25

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.

仅此而已 2024-12-22 02:34:25

使用 *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.

草莓味的萝莉 2024-12-22 02:34:25

我个人更喜欢

char *argv[]

,因为它更好地描述了 argv,IMO。它是一个 char* 数组,即一个字符串数组,这正是我所期望的 argv 。不是双指针。第三种变体看起来不错,与第二种相同。

I personally prefer

char *argv[]

because it describes argv better, IMO. It's an array of char*, that is an array of strings, which is what I expect argv to be. Not a double pointer. The third variant seems ok and is equivalent to the second one.

緦唸λ蓇 2024-12-22 02:34:25

我喜欢 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文