Beginner coder pointer confusion

Hello there, long time lurker, first time poster and was wondering if someone could give me a hand with this small pointer issue.
In the following code I'm implementing a double linked list, I've posted the code which I think is most relevant.
What happens is the debugger tries to check if the pointer *first points to a null value and if so to give it the value of the passed in parameter. This makes sense to me but when I run the code I get an access violation.
Any help appreciated, this really feels like something simple but I can't quite put my finger on it.


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
template <class T>
class DoubleLinkedList {
private:
	typedef DoubleLinkedNode<T> Node;

	//The first node in the list
	Node* first;
	//The last node in the list
	Node* last;
	//The length of the list
	int _length;
public:
	DoubleLinkedList() : first(0), last(0), _length(0){};

	~DoubleLinkedList(){
		while(first->getNext() != (Node*)0){
			first->getNext()->dropNode();
		}
	};
	void append(const T &newelement){
		Node *N = new Node(newelement);

		if(first == (Node*)0) //Problem happens here
		{
			first = N;
			last = N;
			_length = 1;
		}else
		{
			last->insertNodeAfter(N);
			last = N;
			_length++;
		}
	};
};

int main(int argc, char* argv[]){
	string s1("One");

	DoubleLinkedList<string>* L = 0;
	
	L->append(s1);
}
Try ( first == NULL ) or ( first == 0 )
Last edited on
The problem isn't on line 23. The problem is on line 42.

L is a null pointer. L->append() dereferences a null pointer.

1
2
3
4
5
int main()
{
    DoubleLinkedList<string> list ;
    list.append("One") ;
}
cire thank you so much. I've been trying to bunker down on my pointer use but turns out I began using them a bit too much and replacing variables with pointers!
Topic archived. No new replies allowed.