"0xC0000005: Access violation writing location"

Hello. I am having an issue running my program due to a "0xC0000005: Access violation writing location" error. The purpose of the program is to create a binary tree using pointers, and to traverse through the trees with preorder, inorder, and post order algorithms.(The tree does not have to follow the left node being less than the root and the right being greater than the root rule.) Thank You in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using namespace std;


struct  TreeNode
{
	int info;

	TreeNode * left;
	TreeNode * right;
};

void preOrder(TreeNode *p);
void inOrder(TreeNode *p);
void postOrder(TreeNode *p);

int main()
{
	TreeNode* root = new TreeNode;
	TreeNode* node[10];
	int nums[9] = { 2, 7, 5, 2, 6, 9, 5, 11, 4 };
	for (int i = 1; i <= 9; i++)
	{
		
		node[i]->info = nums[i];
		node[i]->left = NULL;
		node[i]->right = NULL;
	}
	root = node[1];

	node[1]->left = node[2];
	node[1]->right = node[3];

	node[2]->left = node[4];
	node[2]->right = node[5];

	node[3]->right = node[6];

	node[5]->left = node[7];
	node[5]->right = node[8];

	node[6]->left = node[9];

	cout << "Part 1" << endl;
	cout << "The pre-order transversal looks like this:     ";
	preOrder(*node);
	cout << "The in-order transversal looks like this:      ";
	inOrder(*node);
	cout << "The post-order transversal looks like this:    ";
	postOrder(*node);

	return 0;
	system("pause");
}

void preOrder(TreeNode *p)
{
	if (p != NULL)
	{
		cout << p->info;
		preOrder(p->left);
		preOrder(p->right);

	}

}

void inOrder(TreeNode *p)
{
	if (p != NULL)
	{
		inOrder(p->left);
		cout << p->info;
		inOrder(p->right);

	}
}

void postOrder(TreeNode *p)
{
	if (p != NULL)
	{
		postOrder(p->left);
		postOrder(p->right);
		cout << p->info;
	}
}
Last edited on
TreeNode* node[10];

node is an array of pointers, none of which point at an actual TreeNode object.

node[i]->info = nums[i];
This is an attempt to dereference one of those pointers, which is pointing somewhere in memory. Could be anywhere. This is bad.

When you make a pointer, you need to also make something for it to point at, and then make it actually point at that object. THEN you can dereference the pointer.
Last edited on
Topic archived. No new replies allowed.