指针数组是C ++中指向指针的指针吗? [英] Is an array-of-pointers a pointer-to-pointer in C++?

查看:107
本文介绍了指针数组是C ++中指向指针的指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



当我写这些函数时:



  void  f( float  * p [ 3 ])
{
cout<< [3]指针浮动的阵列<< ENDL;
}

void f( float ** p)
{
cout<< POINTER TO POINTER<< ENDL;
}





编译器抱怨: void f(float * [])已经有一个正文



发生了什么事?



我的尝试: < br $> b $ b

  void  f( float  * p [ 3 ])
{
cout<< [3]指针浮动的阵列<< ENDL;
}

void f( float ** p)
{
cout<< POINTER TO POINTER<< ENDL;
}

int main()
{
float pos [ 3 ] { 1 .3f, 4 .3f, 5 .3f};

f(& pos); // 错误:无法将float(*)[3]转换为float **
}

解决方案

正如您所看到的,您的前两个函数声明了相同的函数。这类似于:

  void  g( float  p [ 3 ]){ / *   ... * / } 



vs

  void  g( float  * p){ / *   ... * / } 



你可以调用这些函数中的任何一个(假设只有一个被声明和定义),如下所示: />

  float  pos [ 3 ] { 1  .3f, 4  .3f, 5 。 3F}; 
g(pos);





因此,如果你想按声明的方式调用你的函数,你必须这样做:

  float  * arrayOfPointers [ 3  ] = {& pos [ 0 ],& pos [ 1 ],& pos [< span class =code-digit> 2 ]}; 
f(arrayOfPointers);





另一方面,如果你想在代码中调用函数,那么你需要像这样修改声明:

  void  f( float (* p)[ 3 ]){ / *   ... * / } 



这样,你声明一个指向数组的指针。您可能想要使用typedef。

顺便说一下,如果您使用该声明,则数组大小必须匹配,因为它将成为声明的一部分。



此时,您还可以注释原始错误消息显示指向3个浮点数组的指针的语法...



奖金

在C ++中,您也可以使用引用。我更喜欢这样,因为我们不需要在调用站点获取数组的地址,它还允许您确保只有3个项目的数组可以传递给函数。

< pre lang =c ++> void h( float (& p)[ 3 ]){ / * ... * / }
h(pos);





更新

指针数组将是:

  float  f1 =  1  .1f,f2 =  2  .2f,f3 =  3  .3f ; 
float * arrayOfPointers [] = {& f1,& f2,& f3};



内存表示如下:

 [& f1 | & f2 | & f3] 



由于数组等效于指针,因此 float ** 会声明指向第一个项目是指向 float 的指针。



指向浮点数组的指针将是:

  float  arrayOfFloats [] = { 1  .11f, 2  .22f, 3  .33f}; 
float (* pointerToAnArrayOfFloats)[] =& arrayOfFloats;



内存表示形式喜欢:

& [f1 | f2 | f3] 





额外链接

C中的指针和数组等效? - Eli Bendersky的网站 [ ^ ]

有一个很好的图形表示可以帮助你理解差异。



C指针指针,函数指针,指针数组用实例说明 [ ^ ]


指针 - C ++教程 [ ^ ]



此声明一系列花车。

 float pos [3] {1.3f,4.3f,5.3f}; 





你有一个指向浮点数的指针数组,这将被声明:

  float  * pos [ 3 ]; 





所以你声明的就可以了:

 void f(float p [3]) 





 void f(float * p)



不是两者。


Hello.

When I write these functions:

void f(float* p[3])
{
	cout << "ARRAY OF [3] POINTER TO FLOAT" << endl;
}

void f(float** p)
{
	cout << "POINTER TO POINTER" << endl;
}



The compiler complains: void f(float*[]) already has a body

What's going on?

What I have tried:

void f(float* p[3])
{
	cout << "ARRAY OF [3] POINTER TO FLOAT" << endl;
}

void f(float** p)
{
	cout << "POINTER TO POINTER" << endl;
}

int main()
{
   float pos[3] {1.3f, 4.3f, 5.3f};

   f(&pos);  // error: can't convert float(*)[3] to float**
}

解决方案

As you can see, your first 2 functions declare the same function. This would be similar to:

void g(float p[3]) { /* ... */ }


vs

void g(float *p) { /* ... */ }


You can call either of these function (assuming only one is declared and defined) like this:

float pos[3] {1.3f, 4.3f, 5.3f};
g(pos);



Thus if you want to call your functions as declared, you would have to do it like this:

float *arrayOfPointers[3] = { &pos[0], &pos[1], &pos[2] };
f(arrayOfPointers);



On the other hand, if you want to call the function as in your code, then you need to modify the declaration like this:

void f(float (*p)[3]) { /* ... */ }


That way, you declare a pointer to an array. You might want to use a typedef instead.
By the way, if you use that declaration, the array size must match as it will be part of the declaration.

At that point,you can also remarks that your original error message show you the syntax of a pointer to an array of 3 floats...

Bonus
In C++, you can also use references. I prefer that as we don't need to take the address of the array at the calling site and it will also allows you to ensure that only array of 3 items can be passed to the function.

void h(float (&p)[3]) { /* ... */ }
h(pos);



Update
An array of pointer would be:

float f1 = 1.1f, f2 = 2.2f, f3 = 3.3f;
float *arrayOfPointers[] = { &f1, &f2, &f3 };


The memory representation would look like:

[ &f1 | &f2 | &f3 ]


Since an array is equivalent to a pointer, then float ** would declare a pointer to the first item which is a pointer to a float.

A pointer to an array of float would be:

float arrayOfFloats[] = { 1.11f, 2.22f, 3.33f };
float (*pointerToAnArrayOfFloats)[] = &arrayOfFloats;


The memory representation would look like:

& [ f1 | f2 | f3 ]



Extra links
Are pointers and arrays equivalent in C? - Eli Bendersky's website[^]
There is a nice graphical representation that would help you understand the difference.

C Pointer to Pointer, Pointer to Functions, Array of Pointers Explained with Examples[^]


Pointers - C++ Tutorials[^]

This declares an array of floats.

float pos[3] {1.3f, 4.3f, 5.3f};



Nowhere do you have an array of pointers to floats which would be declared thus:

float *pos[3];



So with what you declared you you can have:

void f(float p[3])


OR

void f(float* p)


NOT both.


这篇关于指针数组是C ++中指向指针的指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆