Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing arguments into linked list from dynamic array results in overwriting values
    text
    copied!<p>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 **cmd)</p> <p>After that I need to write certain user input into linked list (for example I need to pass cmd[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)</p> <p>But after the first loop (user entered input, I parsed it then send cmd[1] and cmd[2] the linked list) I do parsing of the string again and then again I send cmd[1] and cmd[2] to the linked list. My linked list however overwrites all previous values 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.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdbool.h&gt; #include &lt;stdlib.h&gt; //struct for environmental variables and thier values 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 // head = malloc(sizeof(struct ListNode)); void printList(); void appendNode(char *v, char *val); int main(int argc, const char * argv[]) { char user_input[20]={0}; int i=0; char c; int count=0;//keep count of words the user entered char ** cmd = NULL;//create pointer and set it to NULL int size = 0; while (strcmp(user_input, "exit") != 0)//check if exit was typed into cmd line, if so then exit { //input three words separated with space scanf("%50[^\n]", user_input);//scan user input size=0; count=0; const char delimiters[] = " !-";//create an array of delimiters to use to separate string char *ptr = strtok (user_input, delimiters); cmd = malloc(sizeof(char));//allocate memory for pointer cmd while (ptr)//split string into tokens to " !-" { cmd = realloc (cmd, sizeof(char*)*(++size));//reallocate more memory for an array if (cmd == NULL) exit (-1); // memory allocation failed cmd[size-1] = ptr; ptr = strtok (NULL, delimiters); count++;//count words in the input } cmd = realloc (cmd, sizeof(char*)*(size+1));// realloc one extra element for the last NULL cmd[size] = 0; for (i = 0; i &lt; (size+1); ++i) printf ("cmd[%d] = %s\n", i, cmd[i]); //free(cmd);//free memory while((c = getchar()) != '\n' &amp;&amp; c != EOF); //flush buffer if (count==3 )//set environmental variable { appendNode(cmd[1], cmd[2]); } printList(); } free(cmd); 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-&gt;var=v;//put value of v into new variable newNode-&gt;value=val;//put value of val into new value newNode-&gt;next = NULL; //if there are no nodes in the list make new node th efirst node if(!head) { head = newNode; } else { //initialize nodePtr to head nodePtr = head; //find the last node in the list while (nodePtr-&gt;next) { nodePtr = nodePtr-&gt;next; } //insert new node as the last node nodePtr-&gt;next = newNode; } } void printList() { struct ListNode* nodePtr = head; //position nodePtr at the head //while noePtr points to node, traverse the list while(nodePtr) { //display value printf("%s\n",nodePtr-&gt;var); //move to next node nodePtr = nodePtr-&gt;next; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload