/ /動的割り当て固定長文字列の2次元配列 - c、配列、文​​字列、ポインタ、malloc

動的割り当て固定長文字列の2次元配列 - c、配列、文​​字列、ポインタ、malloc

プログラムでは、入力からスキャンする必要があります

scanf("%s",currWord);

定義されていない数の単語は、定義されていない数の行に入っています。

私は文字列の2次元配列に単語を入れたい。 弦の長さは固定されています [MAX_WORD_LEN+1]

私の考えは:

int row=10 //10 lines for starting
int col=5 //5 words in each line for starting
int i;

typedef char word[MAX_WORD_LEN+1]; //new type of 11char lenght string
word** matrix; //2 dimensional array (pointers) with no memory

matrix = malloc(row*sizeof(word*)); //allocate row number of word* (word pointer) size

for(i=0;i<row;i++)
{
matrix[i] = malloc(col*sizeof(word)); //allocate col number of words to each row
}

だから、私はそれが正しいかどうか分かりません。

私はいくつかの助けとヒントに満足しています..

編集:

入力から単語を受け取るとき、私は必要に応じてメモリ(行の数と各行の数)を増やす必要があります、どうすればいいですか? (realloc?)

私は次のことをする必要があります:

画像

回答:

回答№1は1

詳細に目を通すことなく、最も簡単な方法は、マトリックスをLinked-List of Linked-Listsとして使用することです。

struct matrix_field
{
char data [11];
matrix_field * next_field;
};

struct matrix_row
{
matrix_field * first_field;
matrix_row * next_row;
};

struct matrix
{
matrix_node * first_row;
};

あなたのデータはメモリ内で次のようになります。

[+]
|
v
[x]-->[a]-->[b]-->[c]-->
|
v
[y]-->[d]-->[e]-->[f]-->
|
v
[z]-->[g]-->[h]-->[i]-->
|
v

---------------

[+] matrix

[x] .. [z] matrix_row

[a] .. [i] matrix_field