0

コマンドラインプロンプトをリンクリストに読み込むプログラムがあります

one two three 

リストに。を使用してコンパイルしています

gcc -o code code.c 

しかし、実行時の2番目のプロンプトについて

./code one two three 

また、リストの先頭に ./code を追加して、

./codeonetwothree

欲しい時だけ

onetwothree

リンクされたリストに ./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[]){

    list_node the_head = {'\0', NULL};
    int the_count, the_count2;
    for(the_count = 0; the_count < argc; the_count++){
            for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++){
                    char next_char = argv[the_count][the_count2];
                    insert_node(&the_head, next_char);
            }
    }

    print_list(&the_head);
    int nth_node = 3;
    printf("Node at the %d spot: ", nth_node);
    printf("%c \n", the_nth_node(&the_head, nth_node));
    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);
    }

}
int the_nth_node(list_node* head, int index_of)
{
  list_node* current_node = head;
  int count_1 = 0; /* the index of the node we're currently
              looking at */
  while (current_node != NULL)
  {
   if (count_1 == index_of)
      return(current_node->the_char);
   count_1++;
   current_node = current_node->next_node;
  }
}
4

2 に答える 2

1

arg[0] は実行中のファイルの名前です。そのため、外側のループカウンターを 1 で初期化する必要があります

for(the_count = 1; the_count < argc; the_count++){
            for(the_count2 = 0; argv[the_count][the_count2] != '\0'; the_count2++){
                    char next_char = argv[the_count][the_count2];
                    insert_node(&the_head, next_char);
            }
    }
于 2014-04-17T20:29:59.873 に答える
1

argv[0]プログラムの名前です。

プログラムの名前が必要ない場合は、次の場所で処理を開始します。argv[1]

for(the_count = 1; the_count < argc; the_count++){
于 2014-04-17T20:30:21.227 に答える