为什么我不能在C++中声明这个双指针数组

问题描述 投票:0回答:1
void foo(){
        static const int tree1[][2] = {
            {FULL,1}, 
            {FULL,2},
            {EMPTY,4},
            {EMPTY,5},
            {EMPTY,3}
        };
        static const int tree2[][2] = {
            {FULL,1},
            {EMPTY,2},
            {FULL,3},
            {LEFT,4},
            {LEFT,5},
            {EMPTY,6},
            {RIGHT,7},
            {RIGHT,8},
            {EMPTY,9}
        };
        static const int ** whydoesthisnotwork[]= {tree1,tree2};
}

我首先尝试将其声明为单个结构,这显然不起作用,所以然后我尝试使用该代码的许多变体来声明它,但我总是遇到这样的问题

cannot convert ‘const int (*)[2]’ to ‘const int**’ in initialization

const int(*)[2]
const int**
不应该是同一个东西吗?

const int* const*
也不起作用

arrays c++11 memory-management
1个回答
0
投票

感谢您的评论,我的假设是数组内部只不过是一个特殊的

const * type
,恰好指向更大的内存区域,显然我错了。

(一个实际有效的磨损“解决方案”)

void foo(){
        static const int * tree1[] = {
            new int[2]{FULL,1}, 
            new int[2]{FULL,2},
            new int[2]{EMPTY,4},
            new int[2]{EMPTY,5},
            new int[2]{EMPTY,3}
        };
        static const int * tree2[] = {
            new int[2]{FULL,1},
            new int[2]{EMPTY,2},
            new int[2]{FULL,3},
            new int[2]{LEFT,4},
            new int[2]{LEFT,5},
            new int[2]{EMPTY,6},
            new int[2]{RIGHT,7},
            new int[2]{RIGHT,8},
            new int[2]{EMPTY,9}
        };
        static const int ** whydoesthisnotwork[]= {tree1,tree2};
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.