Question

I have a program that takes in strings using the command line prompts argv and argc. I keep getting a segmentation fault when I go to run the code and after much researching, I cannot determine what might be causing this. Maybe how I execute the code is the issue? I am using gcc -o code code.c then ./code one two three with one two three being the strings added to the linked list. Any assistance in determining where my error might be would be great.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

typedef struct list_node_s{
    char the_char;
    struct list_node_s *next_node;
}list_node;

void insert_node(list_node *the_head, char the_char);
void print_list(list_node *the_head);

int main(int argc, char *argv[]){
    char next_char;
    list_node *the_head = NULL;
    insert_node(the_head, next_char);
    the_head->next_node = malloc(sizeof(list_node));
    if(the_head == NULL){
            return 1;
    }

    the_head->the_char = 1;
    the_head->next_node == NULL;
    int the_count, the_count2;
    for(the_count = 0; the_count < sizeof(argv); the_count++){
            for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++){
                    next_char = argv[the_count][the_count2];
                    insert_node(the_head, next_char);
            }
    }

    print_list(the_head);
    return (0);
}

void insert_node(list_node *the_head, char the_char){
    list_node * current_node = the_head;
    while (current_node->next_node != NULL) {
        current_node = current_node->next_node;
    }

    current_node->next_node = malloc(sizeof(list_node));
    current_node->next_node->the_char = the_char;
    current_node->next_node->next_node = NULL;
}

void print_list(list_node *the_head){
    if(the_head == NULL){
            printf("\n");
    }else{
            printf("%c", the_head->the_char);
            print_list(the_head->next_node);
    }

}
Was it helpful?

Solution

Change this:

list_node *the_head = NULL;
insert_node(the_head, next_char);
the_head->next_node = malloc(sizeof(list_node));

to:

list_node the_head = { '\0', NULL };

to initialize the_head to an empty node.

OTHER TIPS

One problem is in this function:

void insert_node(list_node *the_head, char the_char){
    list_node * current_node = the_head;
    while (current_node->next_node != NULL) {
        current_node = current_node->next_node;
    }

    current_node->next_node = malloc(sizeof(list_node));
    current_node->next_node->the_char = the_char;
    current_node->next_node->next_node = NULL;
}

When you call it in main you're basically passing in NULL because you're setting the_head to NULL. You're trying to access current_node->next_node in the while loop conditions, but because of what you're passing in, you're basically doing NULL->next_node.

You need to initialize your head to an empty list_node. Basically since you're using a char as your node element you could set the value of the char to 0x00, which would make it a zero byte. Then that way you know that when you're at that value, you're at the head.

I don't mean to self-promote, but if you want to look at some code for this have a look at this github repo for the Barry_CS-331 Data Structures class. There's C and C++ in there for the Data Structures. I think it might have a list but if not you can use the stack and the queue as an overall example.

I have modified you code, there has some bugs:

1)、the key bug is in this code.

 for(the_count = 0; the_count < sizeof(argv); the_count++)
 {
      for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++)
      {
          next_char = argv[the_count][the_count2];
          insert_node(the_head, next_char);
      }
 }

there some bugs: you cann't use the_count < sizeof(argv), because of the type of argv is char* []; so sizeof(argv) maybe 4 or 8, based on your os.

the right is:

 for(the_count = 1; the_count < argc; the_count++){
    for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++){
      next_char = argv[the_count][the_count2];
      insert_node(the_head, next_char);
    }   
  }

2、this code aose has some bugs:

list_node *the_head = NULL;
insert_node(the_head, next_char);
the_head->next_node = malloc(sizeof(list_node));
if(the_head == NULL){
        return 1;
}

the_head->the_char = 1;
the_head->next_node == NULL;

insert_node(the_head, next_char); is no need, you'd better do the_head->the_char = '\0', because of char 1 is no printable character.

One way:

#include <stdio.h>
#include <stdlib.h>

typedef struct list_node_s{
    char the_char;
    struct list_node_s *next_node;
}list_node;

void insert_node(list_node *the_head, char the_char);
void print_list(list_node *the_head);

int main(int argc, char *argv[]){
    list_node *the_head = NULL;
    int the_count, the_count2;

    for(the_count = 0; the_count < argc; the_count++)
        {
        for(the_count2 = 0; the_count2 < strlen(argv[the_count]); the_count2++)
            insert_node(&the_head, argv[the_count][the_count2]);
        }

    print_list(the_head);
    return (0);
    }

void insert_node(list_node **the_head, char the_char){
    list_node *new_node;
    list_node *tail_node;

    /* Allocate and populate a new node. */
    new_node = malloc(sizeof(list_node));
    new_node->the_char = the_char;
    new_node->next_node = NULL;

   /* Is the_head already initialized? */ 
   if(*the_head)
       {
       /* Yes... find the tail_node. */
       tail_node = *the_head;
       while(tail_node->next)
           tail_node = tail_node->next;

       /* Append the new_node to the end of the list. */
       tail_node->next = new_node;
       return;
       }

    /* the_head was not initialized.  The new_node will be the head node. */
    *the_head = new_node;

    return;
    }

void print_list(list_node *the_head){
    if(the_head == NULL){
        printf("\n");
    }else{
        printf("%c", the_head->the_char);
        print_list(the_head->next_node);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top