0

我有一个使用命令行提示 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

4 回答 4

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_headNULL. 您正在尝试访问current_node->next_nodewhile 循环条件,但由于您传入的内容,您基本上是在做NULL->next_node.

您需要将 head 初始化为空list_node. 基本上,由于您使用 achar作为节点元素,您可以将 char 的值设置为0x00,这将使其为零字节。这样你就知道当你处于那个价值时,你就处于领先地位。

我并不是要自我推销,但如果您想查看一些代码,请查看Barry_CS-331 数据结构类的这个 github 存储库。那里有用于数据结构的 C 和 C++。我认为它可能有一个列表,但如果没有,您可以使用堆栈和队列作为整体示例。

于 2014-04-17T04:19:29.220 回答
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为空节点。

于 2014-04-17T04:36:53.993 回答
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);
    }
}
于 2014-04-17T04:56:02.250 回答
0

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

1)、关键bug在这段代码中。

 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),因为argvis的类型char* [];所以sizeof(argv)也许4或者8,根据你的操作系统。

右边是:

 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、这段代码aose有一些bug:

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',因为 char1是不可打印的字符。

于 2014-04-17T05:22:13.863 回答