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)
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)
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.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
//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 < (size+1); ++i)
printf ("cmd[%d] = %s\n", i, cmd[i]);
//free(cmd);//free memory
while((c = getchar()) != '\n' && 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->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
if(!head)
{
head = newNode;
}
else
{
//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
while(nodePtr)
{
//display value
printf("%s\n",nodePtr->var);
//move to next node
nodePtr = nodePtr->next;
}
}
the problem is due to sending local pointer cmd which will be reallocated every time for new string in main routine.
this can be resolved by modifying appendnode API