我怎样才能返回数组?

编程入门 行业动态 更新时间:2024-10-10 12:24:23
本文介绍了我怎样才能返回数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

我怎样才能返回数组?

本文发布于:2023-11-29 13:25:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!