Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9311093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:16:40+00:00 2026-06-19T01:16:40+00:00

I’m having problem with passing argument to the linked list. I wait for user

  • 0

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;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:16:41+00:00Added an answer on June 19, 2026 at 1:16 am

    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

    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 = malloc(sizeof(v));
        newNode->value = malloc(sizeof(val));   
        strcpy(newNode->var, v);
        strcpy(newNode->value, val);    
        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;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.