2016-11-10 6 views
0

作成することなく、構造体の中にアイテムを追加することができます。どのように私は、文字列などのような整数を格納する構造体を使用していた変数

struct movement { 
    char *direction; 
    int steps; 
}; 

私はこの

struct movement m1= { "right",20 }; 
struct movement m2= { "left" ,10 }; 
を行うことにより、構造体の中にアイテムを追加することができますが

私が達成しようとしている最終的な結果は、ユーザー入力(例えば、「右20」)を収集し、それを構造体に格納することです。変数(m1、m2など)を使用せずに未知数のユーザー入力を構造体に格納するにはどうすればよいのでしょうか?

+0

A [リンクリスト](http://www.thegeekstuff.com/2012/08/c-linked-list-例/)は助けることができます –

+0

上記のことは決して「構造体に項目を追加する」ことはしません。お互いに何もする必要のない2つの別々のstructインスタンスを作成しています。 – usr2564301

+0

構造の配列ですか? – RoadRunner

答えて

1

リンクリストを使用してください。これは、あなたが望むものに最適な再帰的なデータ構造です。ここで

が役立つかもしれない、私はしばらく前に書いたいくつかのサンプルコードです:

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

/* basic linked list structure */ 
typedef struct node node_t; 

struct node { 
    char *direction; 
    int steps; 
    node_t *next; 
}; 

/* pointers to the head and tail of the list */ 
typedef struct { 
    node_t *head; 
    node_t *foot; 
} list_t; 

list_t *initialize_list(void); 
list_t *insert_nodes(list_t *list, char *direction, int steps); 
void free_list(list_t *list); 
node_t *generate_node(void); 
void print_list(list_t *list); 
void exit_if_null(void *ptr, const char *msg); 

int 
main(int argc, char const *argv[]) { 
    list_t *list; 

    /* empty list created */ 
    list = initialize_list(); 

    /* inserting information one a time */ 
    list = insert_nodes(list, "right", 20); 
    list = insert_nodes(list, "left", 10); 

    print_list(list); 

    /* freeing list at the end */ 
    free_list(list); 
    list = NULL; 

    return 0; 
} 

/* function to insert information into a node */ 
list_t 
*insert_nodes(list_t *list, char *direction, int steps) { 

    /* called generate_node() to create a new node */ 
    node_t *new; 
    new = generate_node(); 

    /* puts steps information into node */ 
    new->steps = steps; 

    /* allocates space for direction string */ 
    /* this is needed because *direction is a pointer */ 
    new->direction = malloc(strlen(direction)+1); 

    /* copies direction info into node */ 
    strcpy(new->direction, direction); 

    /* inserting information at the tail of the list */ 
    new->next = NULL; 

    if (list->foot == NULL) { 
     /* first insertion into list */ 
     list->head = list->foot = new; 
    } else { 
     list->foot->next = new; 
     list->foot = new; 
    } 

    /* returns modified list */ 
    return list; 
} 

.* function which generates new nodes */ 
node_t 
*generate_node(void) { 
    node_t *newnode; 

    /* create space for new node */ 
    newnode = malloc(sizeof(*newnode)); 
    exit_if_null(newnode, "Allocation"); 

    /* initialize node info to nothing */ 
    newnode->direction = NULL; 
    newnode->steps = 0; 

    return newnode; 
} 

/* creates the empty linked list */ 
list_t 
*initialize_list(void) { 
    list_t *list; 

    create space for list */ 
    list = malloc(sizeof(*list)); 
    exit_if_null(list, "Allocation"); 

    /* set pointers to NULL */ 
    /* We don't want them pointing at anything yet */ 
    list->head = list->foot = NULL; 

    return list; 
} 

/* function which prints entire list */ 
void 
print_list(list_t *list) { 

    /* start at the head of the list */ 
    node_t *curr = list->head; 

    while (curr) { 
     printf("%s %d\n", curr->direction, curr->steps); 

     /* steps through the list */ 
     curr = curr->next; 
    } 
} 

/* function which frees nodes */ 
void 
free_list(list_t *list) { 
    node_t *curr, *prev; 

    /* start at beginning of list */ 
    curr = list->head; 

    /* frees nodes one at a time */ 
    while(curr) { 
     prev = curr; 
     curr = curr->next; 
     free(prev); 
    } 

    /* frees entire list */ 
    free(list); 
} 

/* function which checks malloc(), and whether enough space was allocated */ 
void 
exit_if_null(void *ptr, const char *msg) { 
    if (!ptr) { 
     printf("Unexpected null pointer: %s\n", msg); 
     exit(EXIT_FAILURE); 
    } 
} 
+0

@ Ryan Chia、このコードは多少役立ちましたか?あなたが望むなら、私はそれをもっと詳しく説明することができます。 – RoadRunner

+0

ありがとう!私は非常にCに新しいので、説明は大きく助けになるだろう:) –

+0

ええ問題ない@ RyanChia。コードにコメントを追加し、インターネットでそれらを調べることもお勧めします。構造体オブジェクト情報を一緒にスレッド化したい場合、非常に便利なデータ構造です。 – RoadRunner

-1

LinkedListを使用して、不定数の移動を格納します。 移動ごとに、リンクリストにノードを作成し、次のポインタを更新します。

struct node { 
    struct movement m; 
    node* next; 
} 
3

独立したstructインスタンスのシーケンスを格納する代わりに、実際には "値を構造体に格納する"ようには聞こえません。ユーザー入力ごとに1つ。これを行うための

3最も基本的な方法は以下のとおりです。

  • そのサイズは、コンパイル時に選択配列、。合理的な入力
  • サイズ設定した(その後、を育てる)実行時に構造インスタンスの
  • リンクリストを配列のため、それは「十分に大きい」作るためにあまりにも難しいことではありません

どれが好きですかは、どちらが最も簡単かと思われるかによって異なります。可能であれば、静かなアピールは常に簡単です。

struct movement movements[10000]; 

などのグローバルレベルでは、簡単に64ビットシステムで120 KBのコストしかかかりません。ただし、これにはdirection文字列のメモリは含まれません。これは、構造体は、「自己完結型になります

enum direction { DIRECTION_LEFT = 0, DIRECTION_RIGHT, DIRECTION_UP, DIRECTION_DOWN }; 

:ものは常に(余りに/「ダウン」おそらく「アップ」と)「右」と「左」から選択している場合は、代わりに列挙型としてそれを表すことができ"と(64ビットシステムでは)列挙型がポインタよりも小さいので、より小さい。

realloc()を使用して動的に配列を拡大することはそれほど難しくありませんが、それは頻繁に使用されるように簡単に見ることができます。

+0

私は完全に異なるアプローチを使用して終了し、達成したいものを達成するために構造体を使用しません。しかし、私はまだこのためのリンクされたリストを試してみたいと思っています。私はオンラインで見てみましたが、 "構造のリンクされたリスト"ではなく "構造を使用したリンクリスト"しか見つかりませんでした。私は、構造物を使って作業をさせようとしているところを調整しようとしましたが、進歩はありませんでした。あなたは正しい方向に私を向けることができますか? –

+0

@ RyanChiaそれはほとんど同じことに聞こえる。 Cのリンクリストはほとんどの場合構造体を使って実装されていますが、それを行うのは自然な方法です。各要素に格納されるものは、ポインタ(任意のもの)またはアプリケーションによって定義された構造のいずれかです。 – unwind

関連する問題