指针作为函数参数数组 [英] array of pointers as function parameter

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

问题描述

我在C / C ++数组和指针的一个基本问题。

说我有:

 富* fooPtrArray [4];

如何通过 fooPtrArray 成一个功能?
我曾尝试:

  INT的getResult(美孚** fooPtrArray){} //失败
INT的getResult(美孚* fooPtrArray []){} //失败

该如何处理指针数组?

编辑:我曾经以为的错误味精是传递了错误的指针数组,但是从所有的答复,我意识到这是别的东西......(指针赋值)

 错误信息:
说明资源路径位置类型不兼容类型分配
`美孚**'到'富* [4]tryPointers.cpp tryPointers行21 C / C ++问题

我不太明白为什么它说:美孚*至*富[4]。如果作为函数参数,他们彼此间的变化,为什么任职期间,它给我的编译错误?

我试图复制与最低code中的错误味精如下:

 的#include<&iostream的GT;使用命名空间std;结构美孚
{
INT ID;
};无效的getResult(美孚** fooPtrArray)
{
COUT<< 我在的getResult<< ENDL;
符* fooPtrArray1 [4];
fooPtrArray1 = fooPtrArray;
}诠释的main()
{
符* fooPtrArray [4];
的getResult(fooPtrArray);
}


解决方案

两个

  INT的getResult(美孚** fooPtrArray)

  INT的getResult(美孚* fooPtrArray [])

以及

  INT的getResult(美孚* fooPtrArray [4])

将工作完全正常(它们都是等价的)。

这不是从你的问题清楚是什么问题。什么故障?

在传递这样说,这通常是有意义与允许数组类型declay为指针类型通常是专门用于允许传递不同大小的数组传递元素计为好,因为招阵列

  INT的getResult(美孚* fooPtrArray [],无符号​​N);
...
符* ARRAY3 [3];
符* array5 [5];
的getResult(ARRAY3,3);
的getResult(array5,5);

但如果你总是要通过严格的4个元素的数组,这可能是一个更好的主意,使用不同类型的指针作为参数

  INT的getResult(符*(* fooPtrArray)[4])

在后一种情况下,函数调用将loook如下

 富*数组[4];
的getResult(安培;数组);

(注意&安培; 运营商应用到数组对象)。

和,最后,由于这个问题被标记为C ++,在后者的情况下的基准,也可以代替使用的指针的

  INT的getResult(美孚*(&安培; fooPtrArray)[4]);
...
富*数组[4];
的getResult(数组);

I have a basic question on array and pointer in C/C++.

Say I have:

Foo* fooPtrArray[4];

How to pass the fooPtrArray into a function? I have tried:

int getResult(Foo** fooPtrArray){}  //  failed
int getResult(Foo* fooPtrArray[]){} // failed

How can I deal with pointer array?

EDIT: I once thought the error msg is from passing the wrong pointer array, but from all the responses, I realize that it's something else... (pointer assignment)

Error msg:
Description Resource Path Location Type incompatible types in assignment of 
`Foo**' to `Foo*[4]' tryPointers.cpp tryPointers line 21 C/C++ Problem

I don't quite get why it says: Foo* * to Foo*[4]. If as function parameter they are inter-change with each other, why during assignment, it give me compilation error?

I tried to duplicate the error msg with minimum code as follows:

#include <iostream>

using namespace std;

struct Foo
{
int id;
};

void getResult(Foo** fooPtrArray)
{
cout << "I am in getResult" << endl;
Foo* fooPtrArray1[4];
fooPtrArray1 = fooPtrArray;
}

int main()
{
Foo* fooPtrArray[4];
getResult(fooPtrArray);
}

解决方案

Both

int getResult(Foo** fooPtrArray)

and

int getResult(Foo* fooPtrArray[])

as well as

int getResult(Foo* fooPtrArray[4])

will work perfectly fine (they are all equivalent).

It is not clear from your question what was the problem. What "failed"?

When passing arrays like that it normally makes sense to pass the element count as well, since the trick with allowing the array type to declay to pointer type is normally used specifically to allow passing arrays of different sizes

int getResult(Foo* fooPtrArray[], unsigned n);
...
Foo* array3[3];
Foo* array5[5];
getResult(array3, 3);
getResult(array5, 5);

But if you always going to pass arrays of strictly 4 elements, it might be a better idea to use a differently-typed pointer as a parameter

int getResult(Foo* (*fooPtrArray)[4])

In the latter case the function call will loook as follows

Foo* array[4];
getResult(&array);

(note the & operator applied to the array object).

And, finally, since this question is tagged as C++, in the latter case a reference can also be used instead of a pointer

int getResult(Foo* (&fooPtrArray)[4]);
...
Foo* array[4];
getResult(array);

这篇关于指针作为函数参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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