Der*_*rek 4 c c++ arrays multidimensional-array
我有一个声明如下的函数:
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
Run Code Online (Sandbox Code Playgroud)
这给了我警告:
Classifier.cpp: In member function ‘unsigned char** Classifier::classify()’:
Classifier.cpp:124: warning: address of local variable ‘chars’ returned
Run Code Online (Sandbox Code Playgroud)
这可以忽略吗?基本上,我的问题是如何返回对函数中定义的数组的引用?
我希望能够做到
unsigned char** someData = classify();
Run Code Online (Sandbox Code Playgroud)
只返回数组,而不是它的地址:
return chars;
Run Code Online (Sandbox Code Playgroud)
&chars是指向指针的指针,但是chars指向指针的指针(你想要的).另外请注意,chars是不是一个数组.指针和数组不是一回事,尽管它们经常混淆.