C程序:使用argv,argc,Segmentation Fault创建链接列表

时间:2014-04-17 04:05:52

标签: c command-line linked-list argv argc

我有一个程序,使用命令行提示字符串提示argv和argc。当我去运行代码时,我不断遇到分段错误,经过大量研究,我无法确定导致这种情况的原因。也许我如何执行代码是问题?我正在使用gcc -o code code.c然后./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);
    }

}

4 个答案:

答案 0 :(得分:1)

这个功能有一个问题:

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

当您在main中致电时,您基本上会传递NULL,因为您正在将the_head设置为NULL。您在while循环条件下尝试访问current_node->next_node,但由于您传入的内容,您基本上正在执行NULL->next_node

您需要将头部初始化为空list_node。基本上,因为您使用char作为节点元素,所以可以将char的值设置为0x00,这将使其成为零字节。那么你就知道,当你达到那个价值时,你就是头脑。

我不是要自我推销,但是如果你想查看一些代码,请查看Barry_CS-331 Data Structures class的这个github回购。数据结构中有C和C ++。我认为它可能有一个列表,但如果没有,你可以使用堆栈和队列作为整体示例。

答案 1 :(得分:1)

改变这个:

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

为:

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

the_head初始化为空节点。

答案 2 :(得分:0)

一种方式:

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

答案 3 :(得分:0)

我修改了你的代码,有一些错误:

1),关键错误在于此代码。

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

有一些错误: 由于the_count < sizeof(argv)的类型为argv,因此您无法使用char* [];所以sizeof(argv)可能是48,基于您的操作系统。

右边是:

 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,这段代码有一些错误:

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);不需要,你最好the_head->the_char = '\0',因为char 1不是可打印的字符。