Like Tree1Likes
  • 1 Post By oogabooga

link list problem, struct and malloc

This is a discussion on link list problem, struct and malloc within the C Programming forums, part of the General Programming Boards category; whats wrong with my code. i get this error while compiling Code: gcc -ggdb -std=c99 -Wall -Werror -c -o dictionary.o ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    link list problem, struct and malloc

    whats wrong with my code. i get this error while compiling
    Code:
    gcc -ggdb -std=c99 -Wall -Werror   -c -o dictionary.o dictionary.c
    dictionary.c: In function 'load':
    dictionary.c:58:21: (32 in the abbreviated code below) error: assignment makes pointer from integer without a cast [-Werror]
    cc1: all warnings being treated as errors

    Code:
    typedef struct node
    {
        char *word;
        struct node *next;
    } node;
    
    bool
    load(const char *dictionary)
    {
        // open dictionary
        FILE *fp = fopen(dictionary, "r");
        if (fp == NULL)
        { 
            printf("Error opening dictionary");
            return 1111;
        }
        
        // link list of words
        node *dict_list = malloc(sizeof(node));
        
        // read current word
        int counter = 0;
        char *word;
        for (char c = fgetc(fp); c != '\0'; c = fgetc(fp)) // loop through chars until end of word
        {
            word[counter] = c; // add chars to current word
            counter++;
        }
        word[counter] = '\0'; // word is finished reading
        
        // save current word
        dict_list->word = *word;
        // dictionary has been loaded
        return true;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Nice code formatting. Refreshing.

    You should return false, not 1111 (but see next point).

    Actually, you should change the return type to Node* since you currently have no way to return the list. In the case of an error, return NULL, otherwise return dict_list.

    Assuming the input file is a text file, you loop condition shouldn't be c != '\0'. If it's one word per line, the loop condition should be c != '\n'. However, it would make more sense to use fgets to read a line at a time.

    You do not malloc any space for word.

    And when you assign word to dict_list->word, you shouldn't be using the asterisk (this is the cause of the error you asked about).

    You should close the file at the end of the function.

    And you're only reading in (and have only allocated space for) a single word whereas I assume you want to read in the whole file.

    BTW, if you're planning to do an efficient search on this list (e.g., a binary search), you should read them into an array instead of a linked list.
    iMalc likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by oogabooga View Post
    BTW, if you're planning to do an efficient search on this list (e.g., a binary search), you should read them into an array instead of a linked list.
    With a sorted list you wouldn't really ever do a binary search. It would be slower than a linear search.
    Even searching a sorted vs non-sorted list at best only improves things by a factor of 2, a factor that would often pale in comparison to the speed improvement of having the items in a contiguous array, as far as just lookups are concerned.

    I'm of the opinion that the only list searching technique that makes significant difference, is performing move-to-front of items found, assuming of course that items lookup frequencies are significantly uneven. Othwise just about any other data structure is better, again just as far as lookups are concerned.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    With a sorted list you wouldn't really ever do a binary search. It would be slower than a linear search.
    Just to make sure we're on the same page, you're saying that you agree with me, right?

    I'm not suggesting that he perform a binary search on the linked list (which would be totally nuts).

    I'm saying that if the OP's goal is to read the entire file of "words" into memory and then perform efficient searches on it (which is just an assumption since the code is so broken), then it's best to NOT use a linked list and instead to use an array so that a binary search can be used.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Ah, thanks for the clarification.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List problem
    By peterderijp in forum C Programming
    Replies: 1
    Last Post: 11-05-2010, 11:49 AM
  2. Link List problem
    By Darkinyuasha1 in forum C Programming
    Replies: 2
    Last Post: 05-08-2009, 07:47 PM
  3. Problem in link list
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 07-16-2008, 01:54 AM
  4. memory problem, maybe malloc struct...
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-11-2008, 08:34 AM
  5. Problem with Link List
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 04-24-2002, 11:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21