gpt4 book ai didi

c++ - 从 C++ 中的函数返回二维数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:46 26 4
gpt4 key购买 nike

我有一个这样声明的函数:

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

这是给我的警告:

Classifier.cpp: In member function ‘unsigned char** Classifier::classify()’:
Classifier.cpp:124: warning: address of local variable ‘chars’ returned

可以忽略吗?基本上,我的问题是如何返回对函数中定义的数组的引用?

我希望能够做到

unsigned char** someData = classify();

最佳答案

只返回数组,而不是它的地址:

return chars;

&chars 是指向指针的指针,但是 chars 是指向指针的指针(你想要什么)。另请注意,chars 不是数组。不是。指针和数组不是一回事,尽管它们经常被混淆。

关于c++ - 从 C++ 中的函数返回二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4650911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com