我怎样才能返回数组? [英] How can I return an array?

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

问题描述

有没有办法从一个函数返回一个数组?更具体地说,我创建了这个功能:

Is there any way to return an array from a function? More specifically, I've created this function:

char bin[8];

for(int i = 7; i >= 0; i--)
{
    int ascii='a';
    if(2^i-ascii >= 0)
    {
    	bin[i]='1';
    	ascii=2^i-ascii;
    }
    else
    {
    	bin[i]='0';
    }
}

和我需要一种方法来返回斌[]

and I need a way to return bin[].

推荐答案

您数组是在栈上分配局部变量。您应该使用新[] 分配它在堆上。然后,你可以说:收益斌; 。要注意的是,你必须与明确地释放它删除[] 当你用它做。

Your array is a local variable allocated on the stack. You should use new [] to allocate it on the heap. Then you can just say: return bin;. Beware that you will have to explicitly free it with delete [] when you are done with it.

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

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