将数组返回函数 [英] returning array to function

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

问题描述

任何人都可以更正此示例程序中的任何错误。我只是试图将一个数组返回到主要的
要打印出来。而且我不是确定如何使用指向数组的指针。返回函数调用。


#include< iostream>

使用命名空间std;


int * GetArray(); //函数原型


void main()

{

int * pArray [100];

int n(0);


pArray [n] = GetArray(); //需要回到这里****


for(n = 0; n< 100; n ++)//然后在这里打印****

cout<< pArray [n]<<" " ;;

cout<<" \ n\ n";

}


int * GetArray( )

{

int * pArray;

int n(0);


pArray = new int [100];


for(n = 0; n< 100; n ++)//这只是按顺序填充数组

pArray [n ] = n;


返回pArray;

}

解决方案

< blockquote class =post_quotes> void main()
{
int * pArray [100];


int * pArray;

int n(0);
pArray [n] = GetArray(); //需要回到这里****




pArray = GetArray();

其余的代码是完美的...... ......


cdg写道:

#include< iostream>
使用命名空间std;

int * GetArray(); //函数原型


显式:GetArray是一个什么都不带的函数,它返回

a指向整数的指针。

(可能这不是你想要的。)


请注意,你不能通过值传递或返回数组。

(或者,在其他单词,传递和返回数组与传递

并返回第一个元素的地址相同)

void main()


int main()

{
int * pArray [100];
pArray是一个包含100个整数指针的数组。

int n(0);

pArray [n] = GetArray(); //需要返回这里****


你想从GetArray()返回一个数组 - 这与返回

a指向第一个元素的指针相同。正确的代码是:


//更改pArray的类型

int * pArray = GetArray();

for (n = 0; n <100; n ++)//然后在这里打印****
cout<< pArray [n]<<" " ;;
cout<<" \ n \ n";
}
int * GetArray()
{
[code snipped]}




旁白:使用std :: vector<>而不是数组。很多好处。
http: //www.parashift.com/c++-faq-lit....html#faq-34.1


cdg写道:

任何人都可以更正此示例程序中的任何错误。我只是试图将一个数组返回main要打印出来。我并不确定如何使用指向数组的指针。返回函数调用。

#include< iostream>
使用命名空间std;

int * GetArray(); //函数原型

void main()
`main''返回int {
int * pArray [100];


这声明了一个包含100个int指针的数组。我怀疑'你不想要什么

(但见下文)......

int n(0);

pArray [ n] = GetArray(); //需要回到这里****


好​​吧,也许它*是*你想要的(虽然我还有我的

怀疑)。
for(n = 0; n< 100; n ++)//然后在这里打印****
cout<< pArray [n]<<"英寸;
这个循环将打印出数组中指针的值

`pArray'',其中pArray [0]是通过调用'new''获得的值。 />
以下 - 剩下的都是垃圾。 cout<<" \ n \ n";
}
int * GetArray()
{
int * pArray;
int n(0);

pArray = new int [100];

for(n = 0; n< 100; n ++)//这只是按顺序填充数组
pArray [n] = n;

返回pArray;
}



现在重写它 - 但首先看看信号。你要么太努力了,要么你很难理解编译错误,并开始制作

你不明白的变化......


HTH,

- g


-

Artie Gold - - 德克萨斯州奥斯汀
http://goldsays.blogspot.com
你不能亲吻*除非你错过**

[* - 保持简单,愚蠢。 ** - 简单,愚蠢。]


Could anyone correct any mistakes in this example program. I am just
trying to return an array back to "main" to be printed out. And I am not
sure how a "pointer to an array" is returned to the function call.

#include <iostream>
using namespace std;

int* GetArray(); //function prototype

void main()
{
int* pArray[100];
int n(0);

pArray[n] = GetArray(); //need to return here****

for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" ";
cout<<"\n\n";
}

int* GetArray()
{
int* pArray;
int n(0);

pArray = new int[100];

for(n=0; n<100; n++) //this just fills the array in order
pArray[n] = n;

return pArray;
}

解决方案

void main()
{
int* pArray[100];
int *pArray ;
int n(0);
pArray[n] = GetArray(); //need to return here****



pArray = GetArray();
Rest of the code is perfect..........


cdg wrote:

#include <iostream>
using namespace std;

int* GetArray(); //function prototype
To be explicit : GetArray is a function which takes nothing and returns
a pointer to an integer.
(Probably that is not what you want. )

Note that you cannot pass or return arrays to/from functions by value.
(Or, in other words, passing and returning arrays is same as passing
and returning the address of the first element)

void main()
int main()
{
int* pArray[100]; pArray is an array of 100 integer pointers.
int n(0);

pArray[n] = GetArray(); //need to return here****
You want to return an array from GetArray() - This is same as returning
a pointer to first element. The correct code would be :

// changing the type of pArray
int* pArray = GetArray();
for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" ";
cout<<"\n\n";
}

int* GetArray()
{ [ code snipped ] }



Aside: Use std::vector<> instead of arrays. Lots of benefits.
http://www.parashift.com/c++-faq-lit....html#faq-34.1


cdg wrote:

Could anyone correct any mistakes in this example program. I am just
trying to return an array back to "main" to be printed out. And I am not
sure how a "pointer to an array" is returned to the function call.

#include <iostream>
using namespace std;

int* GetArray(); //function prototype

void main() `main'' returns int {
int* pArray[100];
This declares an array of 100 pointers to int. I suspect that''s not what
you want (but see below)...
int n(0);

pArray[n] = GetArray(); //need to return here****
All right, perhaps it *was* what you wanted (though I still have my
doubts).
for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" "; This loop will print out the values of the pointers in the array
`pArray'', of which pArray[0] is the value obtained by the call to `new''
below -- and the rest are garbage. cout<<"\n\n";
}

int* GetArray()
{
int* pArray;
int n(0);

pArray = new int[100];

for(n=0; n<100; n++) //this just fills the array in order
pArray[n] = n;

return pArray;
}


Now rewrite it -- but first look at the sig. Either you''re trying too
hard, or you got compile errors you didn''t understand and started making
changes you didn''t understand either...

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can''t KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]


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

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