int main 中的双指针
我仍在尝试了解双指针。
我确实知道在大多数情况下通常如何使用双指针,例如
void foo(char **ptr)
{
// blah
}
int main(void)
{
char *ptr;
foo(&ptr);
}
但是我不知道一个指针与另一个指针的作用有何不同
int main(int argc, char **argv) //Double pointer
int main(int argc, char *argv[]) // Single
I am still trying to understand about doublepointers.
I do know how double pointers are usually used in most cases like
void foo(char **ptr)
{
// blah
}
int main(void)
{
char *ptr;
foo(&ptr);
}
However i have no idea what one does differently than the other
int main(int argc, char **argv) //Double pointer
int main(int argc, char *argv[]) // Single
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当用作函数的参数时,数组指示符
[]
与指针完全相同。main
的两个声明实际上是相同的。有时,两种不同的语法意味着不同的事物,但这不是其中之一。
在这种情况下,这意味着您有一个指针数组。每个指针都指向一个字符数组。
argv[0]
是指向第一个char*
字符串的指针,argv[1]
是指向第二个char* 的指针
字符串等When used as a parameter to a function, an array designator
[]
is exactly the same as a pointer. The two declarations you have formain
are in fact identical.There are times when the two different syntaxes mean different things, but this isn't one of them.
In this case it means you have an array of pointers. Each pointer points to an array of characters.
argv[0]
is a pointer to the firstchar*
string,argv[1]
is a pointer to the secondchar*
string, etc.我感受到你的痛苦!我花了很长时间才说服自己,我应该一视同仁地对待他们。
argv[1] 指向第一个参数,argv[argc-1] 指向最后一个参数。是的,各位神枪手,这是真的,当且仅当 argc > 0.
这是我的公式,我会坚持下去。
I feel your pain! It took me a long time to convince myself that I should treat them exactly the same.
argv[1] points to the first parameter, argv[argc-1] points to the final parameter. Yes, all you sharpshooters, that's true iff argc > 0.
That's my formula and I'm stickin' to it.