0

コマンド ライン プロンプト argv および argc を使用して文字列を取り込むプログラムがあります。コードを実行しようとするとセグメンテーション違反が発生し続け、多くの調査を行った後、何が原因であるかを特定できません。たぶん、コードを実行する方法が問題ですか?私は gcc -o code code.c then ./code one two three を使用しており、 one two three はリンクリストに追加された文字列です。私のエラーがどこにあるのかを判断するための支援は素晴らしいでしょう。

これが私のコードです:

#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

この関数には 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、基本的に渡しています。while ループ条件でアクセスしようとしていますが、渡しているもののために、基本的に. NULLthe_headNULLcurrent_node->next_nodeNULL->next_node

頭を空の に初期化する必要がありますlist_node。基本的charに、ノード要素として a を使用しているため、char の値を に設定でき0x00ます。これにより、ゼロバイトになります。そうすれば、その値にいるときは先頭にいることがわかります。

自己宣伝するつもりはありませんが、このためのコードを見たい場合は、この github repo for the Barry_CS-331 Data Structures classをご覧ください。データ構造には C と C++ があります。リストがあるかもしれないと思いますが、そうでない場合は、スタックとキューを全体的な例として使用できます。

于 2014-04-17T04:19:29.220 に答える
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)のタイプがargvisであるため、 を使用できませんchar* []。あなたのOSに基づいて、そう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、このコード aose にはいくつかのバグがあります。

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

于 2014-04-17T05:22:13.863 に答える