I'm sure I've screwed up my pointers, or perhaps the initial NULL, but I can't figure it out.
I'm trying to write a linked list out to a text file:
write_out(node *ll){
ofstream out;
out.open("output.txt");
if (!out.is_open()) exit(EXIT_FAILURE);
cout << ll->value;
//stuff to write out
}
and:
struct node {
int value;
node *next;
}
But the line cout << ll->value causes Segmentation fault: 11, I do not understand why however.
I've commented out the code I was actually doing to write out, as this is irrelevant, the issue is obviously with my (lack) of understanding how the above works.
I call write_out(linkedlist) where node* linkedlist points to the first node.
This happens after:
read_in(node *ll){
ifstream data; //opened and checked open as above for out
int v;
ll = new node;
node *tmp = ll;
data >> tmp->value;
while(data >> v){
tmp->next = new node;
tmp = tmp->next;
tmp->value = v;
}
tmp->next = NULL; //thanks @sharth
}
Which surely hasn't left ll = NULL?
write_out? It sounds likelinkedlistis a null or invalid pointer.node* linkedlist = NULL, thenread_in, and thenwrite_out. I'll postread_inabove, in case that is leaving it asNULL.nextis null rather than pointing to the nth.. can you see why?