I am trying to write a code that reads a text file to a linked list and stores in memory. I don't know why there are errors on malloc
functions I use it in my code.
This is the pre-given header file that need to keep as is:
#ifndef ADDRESSBOOK_LIST_H
#define ADDRESSBOOK_LIST_H
#define NULL_SPACE 1
#define NAME_LENGTH (20 + NULL_SPACE)
#define TELEPHONE_LENGTH (10 + NULL_SPACE)
typedef struct telephoneBookNode
{
int id;
char name[NAME_LENGTH];
char telephone[TELEPHONE_LENGTH];
struct telephoneBookNode * previousNode;
struct telephoneBookNode * nextNode;
} TelephoneBookNode;
typedef struct telephoneBookList
{
TelephoneBookNode * head;
TelephoneBookNode * tail;
TelephoneBookNode * current;
unsigned size;
} TelephoneBookList;
This is the code I write to load txt file into memory:
The entry
has a format ID, Name, Number
like 123, Alice, 0123456789
#include "addressbook_list.h"
TelephoneBookList * createTelephoneBookList(char entry[])
{
TelephoneBookList* aList = NULL;
TelephoneBookNode* aNode = createTelephoneBookNode();
char *tokens;
tokens = strtok(entry, ", ");
aNode->id = tokens;
tokens = strtok(NULL, ", ");
aNode->name = tokens; //Error: array type char[21] is not assignable
tokens = strtok(NULL, ", ");
aNode->telephone = tokens; //Error; array type char[11] is not assignable
aNode->nextNode = aList->head;
aList->head = aNode;
if (aList == NULL)
{
aNode->nextNode = NULL;
aNode->previousNode = NULL;
aList->current = aNode;
aList->head = aNode;
aList->tail = aNode;
}
else
{
aList->tail->nextNode = aNode;
aNode->nextNode = NULL;
aList->tail = aList->tail->nextNode;
}
return aList;
}
This is the function to create nodes, I got error:
incompatible pointer to integer conversion assigning to 'char' from 'char*', dereferenced with *
TelephoneBookNode * createTelephoneBookNode()
{
TelephoneBookNode* aNode;
aNode = (TelephoneBookNode*) malloc(sizeof aNode);
aNode->id = (int) malloc(sizeof aNode->id);
aNode->name = (char*) malloc(sizeof aNode->name);
aNode->telephone = (char*) malloc(sizeof aNode->telephone);
return aNode;
}
Please someone can explain me a bit on the error. Thank you very much!