I am trying to have a linked list in a header file. the structure is also defined there as well. I am trying to read it in the main file.
- "linkedlist.h"
#include<stdio.h>#include<stdlib.h>typedef struct ll{ int number; char *data; bool flag; struct ll *next;} ll;ll* def_variables(){ ll *head = NULL; ll *first = NULL; ll *second = NULL; first->number = 20; first->data = "ding dong"; first->flag = true; first->next = second; second->number = 21; second->data = "dong ding"; second->flag = false; second->next=NULL; head = first; return head;}
- main.cpp
#include<stdio.h>#include<stdlib.h>#include "linkedlist.h"using namespace std;int main(){ ll *iterator; iterator = def_variables(); while(iterator->next !=NULL) { printf("%s is text\n",iterator->data); printf("%d is number\n",iterator->number); printf("%d is flag\n",iterator->flag); iterator=iterator->next; }}
It compiles, but gives segmentation fault
. I am at a loss. Any thoughts are most welcome.
I first put the linked list as an object, and it threw errors. Then I enclosed it in a function that would return the head. However, now there is a segmentation fault.