Search code examples
cpointersauto-increment

C increment counter in struct


I'm doing a project where I read words from a file, add them into a linked list and then count the frequencies in which the words occur. My program is reading the words into the linked list, but it's not incrementing the count every time a duplicate word occurs- the count is staying at 1. I'm not going to paste my entire code, just the parts that apply.

struct node {
    struct node *next;
    char word[60];
    int wordCount;
};

And the push function:

void push_front (struct node **list, char * n) {

assert (list);
int count = 0;

struct node *temp = (struct node *)malloc (sizeof (struct node));

if (!temp) {
    fprintf (stderr, "Out of memory\n");
    exit (1);
}
if (list == NULL) {
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount+=1;
    *list = temp;
} else {
    while (list) {
        if (temp->word == n) {
            temp->wordCount+=1;
            exit(1);
        } else {
            temp->next = *list;
            strcpy(temp->word, n);
            temp->wordCount+=1;
            *list = temp;
            break;
        }
    }
}
return;
}

An example run of this program would be:

Word [0] = you, 1
Word [1] = you, 1
Word [2] = are, 1
Word [3] = a, 1
Word [4] = whipped, 1
Word [5] = what, 1
Word [6] = what, 1
Word [7] = you, 1
Word [8] = world, 1
Word [9] = you, 1
Word [10] = hello, 1

Now, as you can see the counter at the end of each line is staying at 1, however with each duplicate word it should increment, and duplicate words should also not be added to the linked list. I'm sorry about this, I'm new to C!

Regards


Solution

  • The following comparison

    if (temp->word == n) { 
    

    is a comparison of pointers (addresses) and not comparison of strings

    String comparison in C should not done in the above way

    You could use the strcmp from #include <string.h>:

    if (strcmp(temp->word, n)==0) {
    

    And your function contains some bugs to fix. I re-work your function:

    void push_front (struct node **list, char * n) {    
        assert (list);
        struct node *temp = *list;
    
        while (temp) {
            if (strcmp(temp->word, n) == 0) {
                temp->wordCount+=1;
                return;
            }
            temp = temp->next;
        }
    
        temp = (struct node *)malloc (sizeof (struct node));
        if (!temp) {
            fprintf (stderr, "Out of memory\n");
            exit (1);
        }
        temp->next = *list;
        strcpy(temp->word, n);
        temp->wordCount=1;
        *list = temp;
        return;
    
    }
    

    in the main() your function should called in this way

    void main() {
    
        node *head = NULL;
    
        push_front (&head, "toto");
        push_front (&head, "titi");
        push_front (&head, "toto");
        push_front (&head, "titi");
    
        node *tmp;
        int i=0;
        for (tmp=head; tmp!=NULL; tmp = tmp->next)
            printf("Word[%d] = %s, %d\n", i++, tmp->word, tmp->wordCount);
    
    }
    

    I tested it and the execution gave the following output:

    $ ./test
    Word[0] = titi, 2
    Word[1] = toto, 2