Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 646

How do I get my linked list passed from header file to main?

$
0
0

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.


Viewing all articles
Browse latest Browse all 646

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>