2

I want to create a linked list in C in which the user can input strings which will be stored as nodes in the list. This is my node structure:

typdef struct NODE {
    char word[50];
    struct NODE* next;
} node;

From my main method I want to prompt the user to enter a strings and subsequently call a method that adds the string to the linked list (but does not include any characters following a space), and do that repeatedly until the user enters a specific string which terminates the process, so in my main method I have:

void main(){
    node* fullList = NULL;
    char stopString[5];
    sprintf(stopString, "stop"); 
    char string[50];
    printf("Enter a word: ");
    scanf("%[^ ]s", string);
    while (strcmp(string, stopString) != 0) {
         addToLinkedList(fullList, string);   
         printf("Enter a word: ");
         scanf("%[^ ]s", string);
    }
}

This is my addition method:

void addToLinkedList(node* list, char str[]) {
    node* freeSpot;
    node* newNode;

    freeSpot = list;
    if (list == NULL){
            freeSpot = freeSpot->next;
    }

    newNode = (node *)malloc(sizeof(node));

    newNode->word = str;
    //strcpy(nweNode->next, str);
    newNode->next = NULL;
    freeSpot->next = newNode;

}

BUT I get an error:

"incompatible types when assigning to type âchar[256]â from type âchar *â"

and if I replace "newNode->word = str;" with the piece of code commented out below, I get:

warning: passing argument 1 of âstrcpyâ from incompatible pointer type [enabled by default]
/usr/include/string.h:128:14: note: expected âchar * __restrict__â but argument is of type
âstruct NODE *â

I'm pretty stuck at this point, I'm not sure how to implement this successfully; any suggestions?

4
  • and if (list == NULL){ --> while(freeSpot->next != NULL){ Commented Nov 14, 2014 at 21:31
  • 1
    for scanf you don't need the extra s, this is fine scanf("%s", string); %s stops reading after a space character is read. Also note that since you are not allocating new memory for your string inputs, all strings in the list will contain the same value Commented Nov 14, 2014 at 21:32
  • void addToLinkedList(node* list, char str[]) { --> void addToLinkedList(node** list, char str[]) { Commented Nov 14, 2014 at 21:32
  • and if(*list == NULL) { *list = newNode;} Commented Nov 14, 2014 at 21:39

2 Answers 2

1

This error is related to the commented line. Have you saved your file after commenting this out? Have you cleaned the project? Anyway, this line: newNode->word = str; is going to cause troubles as well. Use strcpy instead. You want to copy string, not the pointer.

Sign up to request clarification or add additional context in comments.

Comments

0

Remove:

newNode->word = str;

And add:

strcpy(nweNode->word, str);

You are copying to the member word of the current node, not to the next node.

You should check that str is not longer than (50-1) before copying.

1 Comment

Wow, thanks, can't believe I didn't catch such a silly mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.