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 8211983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:36:42+00:00 2026-06-07T10:36:42+00:00

This is just a rough code,so there are no free(s) in there yet. I

  • 0

This is just a rough code,so there are no free(s) in there yet.
I am just trying to figure out where its messing up my linked list.

The purpose of the following function is to accept something like :

add 1 2

or

add 1 "some quote" maybe more stuff

and make a linked list with elements..
in first case:

[add]->[1]->[2]

in second case:

[add]->[1]->["some quote" maybe more stuff]

I know that it is actually doing the steps, because the ‘count/total’ is right in the output. However when I try to iterate through the linked list, it only prints the first element.

typedef struct command{
    char* args;
    struct command *next;
}command;
typedef struct commands_list{
    command *head;  /*Start of the queue*/
    int total;  /*Total commands passed*/
}commands_list;

commands_list* process_command(char *command){
    char curr_char;                 /*Keeps track of current character*/
    int start_pos;
    int i;
    int len;    

    /*Length of user input*/
    int quote=0;
    int empty =1;
    commands_list *commands;
    struct command *conductor;

    len = strlen(command);              /*Calculate length*/    
    /*Initialize the List*/
    commands=malloc(sizeof(commands_list));         /*Allocate memory for the linked list*/
    commands->head = malloc(sizeof(struct command));
    conductor = commands->head;

    for(i=0,start_pos=0;i<strlen(command);i++){
        curr_char = command[i];
        if (empty==0){
            conductor = malloc(sizeof(struct command));
        }
        if (curr_char == ' '){      /*If there was a space found copy the stuff before the space*/
            if ( i>0 && command[i-1]==' ') {
                start_pos++;
                continue;
            }
            conductor->args = malloc(i-start_pos+1*(sizeof(char))); /*Allocate memory for the word to be copied*/
            strncpy(conductor->args,command+start_pos,i-start_pos); /*Copy the word/command to the memory allocated*/
            conductor->args[i-start_pos+1]='\0';            /*Add null terminator at end*/
            commands->total++;              /*Increase total # of commands*/
            conductor=conductor->next;          /*Conductor points to the first element now*/
            start_pos =i+1;
            if (empty==1){
                empty=0;
            }
        }
        else if (curr_char == '\"'){        /*If a quote was found, copy the rest of the string and exit loop*/
            conductor->args = malloc(len-i+1*(sizeof(char)));
            strncpy(conductor->args,command+i,len-i);
            conductor->args[len-i+1]='\0';
            conductor->next=NULL;
            commands->total++;
            quote=1;
            //empty_queue = 0;
            conductor = conductor->next;
            if (empty==1){
                empty=0;
            }
            break;
        }
    }
    if (quote==0){                  /*If there was no quote in the string, get the last element*/
        if (empty==0){
            conductor = malloc(sizeof(struct command));
        }
        conductor->args = malloc(len-start_pos+1*(sizeof (char)));
        strncpy(conductor->args,command+start_pos,len-start_pos);
        conductor->args[len-start_pos+1]='\0';
        conductor->next=NULL;
        commands->total++;
    }   /*Finish find quote*/
    printf("%d commands found\n",commands->total);
    //free(conductor);
    return commands;    
}

And a temporary method that I am using to print the linked list:

int print_list(commands_list **headNode){
    commands_list *top = *headNode;
    struct command *temp = top->head;           /*Temporary variable for command*/
    while(temp!=NULL){
        printf("I was here to print: [%s]\n",temp->args);
        temp = temp->next;
    }
    printf("It was all null\n");

    free(temp);
}

Thanks

  • 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-07T10:36:44+00:00Added an answer on June 7, 2026 at 10:36 am

    There are many problems in the code that make it hard to debug.

    The main issue is how you add a struct command * to the end of the list. The line

            conductor=conductor->next;
    

    will just assign conductor to NULL (or whatever malloc did). You never assign conductor->next to anything.

    When you malloc a new struct command *, you need to update the old conductor->next to the newly allocated element. So instead of:

            conductor = malloc(sizeof(struct command));
    

    You need something like:

            struct command *tmp = malloc(sizeof(struct command));
            conductor->next = tmp;
            conductor = tmp;
    

    Also, it might help your debug if you added the line
    conductor->args = “not allocated yet #1“;
    after the last line above. This is gross, and should not appear in production code, but will help you debug your issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I fear this just may not be possible, but I'm trying to create a
So for some reason this just doesn't make sense to me. What Im trying
I assume this just returns an int. Is there anything else going on I
Ok, I've got a list like this (just a sample of data): data =
I'm trying to extrude a path in 3d. Nothing fancy yet, just following some
This just started happening three weeks or so ago. The content of my website
This just saves time. Since I already have a web applciation. I can just
I'm hoping this just needs a new pair of eyes casting over it. I
I understand this is an easy question but for some reason this just isn't
I don't think that this could be done in C#, but posting this just

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.