本文介绍了双指针和指针数组之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通的c/c ++程序中,我们将主函数编写为任意一个

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 representdouble 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 **.

始终记住,数组和指针是两种不同的类型.指针和数组 equivalence 表示指针算术和数组索引是等效的.

Always remember that arrays and pointers are two different types. The pointers and arrays equivalence means pointer arithmetic and array indexing are equivalent.

建议阅读:

  • But I heard that char a[] was identical to char *a.
  • Why are array and pointer declarations interchangeable as function formal parameters?

这篇关于双指针和指针数组之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:47