Hello, I recently started learning C and I encountered some problems with writing a good doubly linked list. I have done this in Java before, but pointers and memory management causes me some pain.
So I'm trying to write a simple dictionary like thing using linked list. When I declare my dict_entry struct in one main.c file w/o header files and a separate file for functions' implementation everything works pretty, but when I'm trying to do virtually the same thing with a header file, function implementation file, etc. It breaks down and I don't understand why.
The code below should only work in case when the list is empty.
Header file:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
char *key;
char *value;
void *next_entry;
void *previous_entry;
}dict_entry;
// ADD ENTRY TO THE DICTIONARY
int add_entry( dict_entry*, char*, char* );
Imprementation file:
Code:
#include "dict_lib.h"
// Return: SUCCES at successful entry addition
int add_entry( dict_entry *dictionary, char *new_key, char *new_value )
{
if( dictionary == NULL )
{
dict_entry* new_entry = malloc(sizeof(dict_entry));
new_entry->key = new_key;
new_entry->value = new_value;
new_entry->next_entry = NULL;
new_entry->previous_entry = NULL;
dictionary = new_entry;
new_entry = NULL;
}
return 0;
}
Main file:
Code:
#include "dict_lib.h"
void main()
{
dict_entry *dictionary = NULL;
char *a = (char*)malloc(10);
char *b = (char*)malloc(10);
a = "some";
b = "text";
add_entry( dictionary, a, b );
}