Alrighty, my first post. Here we go.
This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char *str;
struct node *next;
}node;
typedef struct List {
node *head;
}List;
List l1, l2;
void add(List strList, char *string){
if(strList.head != NULL){
strList.head->next = strList.head;
}
strList.head->str = string;
printf("\n%s\n", strList.head->str);
}
... etc etc
It compiles without a problem, but gives me a segmentation fault when I run it. Through endless "printf("\nklsdfjkl\n");" I've found the problem is in the "strList.head->str = string;". I know it has to to with memory and the *str array in the list nodes, but I don't know how to fix it. If I try to put "char *str = (char *)malloc(80 * sizeof(char));" in the struct, it gives a lot of wierd errors all over the place. Any help would be appreciated. Thanks!
This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char *str;
struct node *next;
}node;
typedef struct List {
node *head;
}List;
List l1, l2;
void add(List strList, char *string){
if(strList.head != NULL){
strList.head->next = strList.head;
}
strList.head->str = string;
printf("\n%s\n", strList.head->str);
}
... etc etc
It compiles without a problem, but gives me a segmentation fault when I run it. Through endless "printf("\nklsdfjkl\n");" I've found the problem is in the "strList.head->str = string;". I know it has to to with memory and the *str array in the list nodes, but I don't know how to fix it. If I try to put "char *str = (char *)malloc(80 * sizeof(char));" in the struct, it gives a lot of wierd errors all over the place. Any help would be appreciated. Thanks!