خرید بک لینک

Vote count: 0

I am trying to write a code that read a text file to a linked list and store in memory. I dont know why there are error on malloc functions I use 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;
    }

    retu 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);

    retu aNode;
}

Please someone can explain me a bit on the error. Thank you very much!

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 16 ارديبهشت 1395 ساعت: 11:38

صفحه بندی