从功能用C返回二维数组++ [英] return 2d array from function in C++
本文介绍了从功能用C返回二维数组++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经声明,像这样一个功能:
unsigned char型**分类::分类(){
// ...
unsigned char型**字符=新的无符号的char * [H]
的for(int i = 0; I< H,我++)
字符由[i] =新的无符号字符[W * 3]。// ...返回&安培;字符;
//注意:这是返回字符;我得到以下几点:不能转换'无符号字符*'到'unsigned char型**'的回报
这是给我的警告:
Classifier.cpp:在成员函数'unsigned char型**分类::分类()':
Classifier.cpp:124:警告:局部变量的地址'字符'返回
这是确定以忽略?基本上,我的问题是你如何返回引用到在函数中定义的数组?
我希望能够做到
unsigned char型** someData =分类();
解决方案
这是的从不没关系忽略。你返回一个局部变量的地址。当你离开区分该地址将变得无效()
的堆栈帧,的前的调用者有机会使用它。
您只需要返回该变量的值,而不是:
返回字符;
I have a function declared like so:
unsigned char** Classifier::classify(){
//...
unsigned char **chars = new unsigned char *[H];
for(int i = 0; i < H; i++)
chars[i] = new unsigned char[W*3];
//...
return &chars;
//note: when this is "return chars;" I get the following: cannot convert ‘unsigned char*’ to ‘unsigned char**’ in return
This is giving me the warning:
Classifier.cpp: In member function ‘unsigned char** Classifier::classify()’:
Classifier.cpp:124: warning: address of local variable ‘chars’ returned
Is this ok to ignore? Basically, my question is how do you return a reference to an array that is defined in the function?
I want to be able to do
unsigned char** someData = classify();
解决方案
This is never okay to ignore. You're returning the address of a local variable. That address will become invalid when you leave classify()
's stack frame, before the caller has a chance to use it.
You only need to return the value of that variable instead:
return chars;
这篇关于从功能用C返回二维数组++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文