Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何动态创建指针数组?_C++_Arrays_Pointers - Fatal编程技术网

C++ 如何动态创建指针数组?

C++ 如何动态创建指针数组?,c++,arrays,pointers,C++,Arrays,Pointers,我正在尝试动态创建一个大小为“n”的数组,该数组可以包含指向多个邻接列表的指针。但是,当我运行代码时,它不会产生节点数组,它只会产生以下结果: 它不是一个数组,它只是一个节点 这是我的密码: main.cpp #include "graph.h" using namespace std; int main() { graph g; return 0; } graph.cpp #include "graph.h" // Constructor graph::graph()

我正在尝试动态创建一个大小为“n”的数组,该数组可以包含指向多个邻接列表的指针。但是,当我运行代码时,它不会产生节点数组,它只会产生以下结果:

它不是一个数组,它只是一个节点

这是我的密码:

main.cpp

#include "graph.h"
using namespace std;

int main() {
    graph g;
    return 0;
}
graph.cpp

#include "graph.h"

// Constructor
graph::graph()
{
    int size = 5; // 
    // Allocate array of size (n) and fill each with null
    adjListArray = new node*[size];

    // Set each entry in array to NULL
    for (int i = 0; i < size; i++)
    {
        adjListArray[i] = NULL;
    }
}
#包括“graph.h”
//建造师
graph::graph()
{
int size=5;//
//分配大小为(n)的数组,并用null填充每个数组
adjListArray=新节点*[size];
//将数组中的每个项设置为NULL
对于(int i=0;i
图h

#include<cassert>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct node
{
    int name;
    node *next;
};

class graph
{
    public:
        // Constructors/destructors
        graph();
        ~graph();

    private:
        node** adjListArray; // Array of pointers to each adjacency list
};
#包括
#包括
#包括
#包括
使用名称空间std;
结构节点
{
int名称;
节点*下一步;
};
类图
{
公众:
//构造函数/析构函数
图();
~graph();
私人:
节点**邻接列表数组;//指向每个邻接列表的指针数组
};

我哪里出错了?

问题是,当您只有一个指向内存开头的指针时,无法判断内存块有多大,因此您的IDE只假设它的大小是
sizeof(datatype)
,并且只显示第一个元素

我看不出有什么问题。这可能是调试器如何表示数据的问题。。。以及是否可以强制它将adjListArray显示为数组。请记住,您正在显示的属性不是声明为数组,而是声明为
节点**
我真愚蠢,盲目信任调试器,而不亲自测试这些值是否存在。问题解决了,谢谢。