strizh 0 Newbie Poster

I'm having problem with passing argument to the linked list.
I wait for user input then I parse this user input and put parsed tokens into dynamic array (char **c)

After that I need to write ceratin user input into linked list (for example I need to pass c[1] to the linked list)
And this process repeats in a while loop until user enters "exit" (so user will be asked for another input and then I need to parse it again and so on)

But after the first loop (user entered input, I parsed it then send c[1] and c[2] the linked list) I do parsing of the string again and then again I send c[1] and c[2] to the linked list. My linked list however overwrites all previous valus to the new ones and then adds new node with the new values so all my nodes now have the same value.
I just started learning c month ago, so I probably there is a problem with pointers or maybe it's linked list, even though I wrote this linked list in c++ and now I converted it to there could me some mistakes after my conversion. I also tested my linked list with just passing regular arguments not from dynamic array and it's seem to work fine.

char **c=NULL;
int size=0;

struct ListNode
    //variable and value in the node
    char *var,*value;
    struct ListNode* next;//point to the next node
struct ListNode* head;//pointer to the head

void appendNode(char *v, char *val);

void printList();

int main(int argc, const char * argv[])

while (strcmp(user_input, "exit") != 0)//check if exit was typed if so then exit

       scanf("%50[^\n]", user_input);//scan user input into cmd

        const char delimiters[] = " !-";//create an array of delimiters to use to separate string

        char *ptr = strtok (user_input, delimiters);

        c = malloc(sizeof(char));//allocate memory for pointer command

        while (ptr)//split string into tokens to " !-"
            c = realloc (c, sizeof(char*)*(++size));//reallocate more memory for an array

            if (c == NULL)
                exit (-1); // memory allocation failed

            c[size-1] = ptr;

            ptr = strtok (NULL, delimiters);

            count++;//count words in the input

        c = realloc (c, sizeof(char*)*(size+1));// realloc one extra element for the last NULL
        c[size] = 0;

        for (i = 0; i < (size+1); ++i)
            printf ("c[%d] = %s\n", i, c[i]);//print parsed string

        free(c);//free memory

        while((c = getchar()) != '\n' && c != EOF); //flush buffe

        if (strcmp(user_input,"exit")==0)
            printf("you exited");

           if (count==3 )

                 appendNode(command[1], command[2]);


    free(command);//free memory
return 0;

void appendNode(char *v, char *val)
    struct ListNode* newNode;//to point to new node
    struct ListNode* nodePtr = NULL;//to move through the list

    //allocate new node and store int there
    newNode = malloc (sizeof( struct ListNode));
    newNode->var=v;//put value of v into new variable
    newNode->value=val;//put value of val into new value
    newNode->next = NULL;
    //if there are no nodes in the list make new node th efirst node
        head = newNode;

        //initialize nodePtr to head
        nodePtr = head;
        //find the last node in the list
        while (nodePtr->next)
            nodePtr = nodePtr->next;
        //insert new node as the last node
        nodePtr->next = newNode;

void printList()
   struct ListNode* nodePtr = head; //position nodePtr at the head
    //while noePtr points to node, traverse the list
        //display value
        //move to next node
        nodePtr = nodePtr->next;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.