双指针和指针数组的区别 [英] Difference between double pointer and array of pointers
问题描述
在普通的 c/c++ 程序中,我们将 main 函数编写为其中之一
In a normal c/c++ program we write main function as either
int main(int c, char **argv)
或
int main(int c, char *argv[])
这里 argv 表示一个指针数组,但我们甚至表示使用**的双指针(指向指针的指针).
Here argv represents an array of pointers but we even represent double pointer(pointer to pointer) using **.
例如:
char p,*q,**r;
q=&p;
r=&q;
这里的 r 是一个双指针而不是指针数组.
Here r is a double pointer and not array of pointers.
谁能解释一下其中的区别?
Can any one explain the difference?
推荐答案
当用作函数参数时
char a[] // compiler interpret it as pointer to char
相当于
char *a
同样,在main
的签名中,char *argv[]
等价于char **argv
.请注意,在 char *argv[]
和 char **argv
两种情况下,argv
都是 char **
(不是指针数组!).
and similarly, in main
's signature, char *argv[]
is equivalent to char **argv
. Note that in both of the cases char *argv[]
and char **argv
, argv
is of type char **
(not an array of pointers!).
声明不一样
char **r;
char *a[10];
在这种情况下,r
是指向 char
指针的指针类型,而 a
是指向 char 指针的数组类型
.
任务
In this case, r
is of type pointer to pointer to char
while a
is of type array of pointers to char
.
The assignment
r = a; // equivalent to r = &a[0] => r = &*(a + 0) => r = a
是有效的,因为在此表达式中,数组类型 a
将再次转换为指向其第一个元素的指针,因此类型为 char **
.
is valid because in this expression again array type a
will be converted to pointer to its first element and hence of the type char **
.
永远记住数组和指针是两种不同的类型.指针和数组等价意味着指针算术和数组索引是等价的.
Always remember that arrays and pointers are two different types. The pointers and arrays equivalence means pointer arithmetic and array indexing are equivalent.
推荐阅读:
这篇关于双指针和指针数组的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!