So I have a Node struct
struct Node
{
int x = 0;
};
I make 20 Node*
s. My understanding is that Node**
is a pointer to the start of an array that holds pointers to Node
s.
constexpr int mazeSize = 20;
Node** testMaze = new Node * [mazeSize];
After this, I started getting warnings and errors when I tried to do anything with it. Examples:
testMaze[0]->position.x == 0; //->Using uninitialized memory `*testMaze` and
What I understood from this error: *testMaze
is dereferencing the array of pointers, which means it's referring to the first Node
object in that array. If this is the case than how would I initialize it? If I simply created the Node*
as so:
Node* node = new Node;
node->x = 0;
Than it works fine and there is no need to initialize it, so why not with the way I am doing it? I also don't understand how to initialize a struct.
Another examlpe:
testMaze[0]->x == testMaze[1]->x //->Runtime error: Access violation reading error
testMaze[0]->x = 0; //->Runtime error: Access violation writing error
How can I fix these problems? Thanks.
new Node * [mazeSize];
provides an array of pointers. You haven't shown any code that points those pointers anywhere valid, and the warning message suggests that you didn't write such code. Pointer's gonna point, so the solution is create a fewNode
s and start pointing.Note*
is not aNode
. You have pointers but noNode
to point to. You probably meant to usenew Node[mazesize]
to actually createNode
objects but you should just usestd::vector<Node>
instead.new Node * [mazeSize];
would give me pointers with nodes created and pointed to by those pointers, but it actually only makes the pointers and I have to create the nodes myself and get those pointer to point to those nodes. Is that it?