0

こんにちは、構造体で strlen 関数を使用しようとしていますが、ひどく失敗しています。私のコードは次のとおりです。scanfの後、テーブルの最初の文字のみを取得し、最後の印刷でプログラムがクラッシュします。私が間違っている提案やアイデアはありますか? 主に、ユーザーはテーブルに英数字を入力し、strlen を使用して英数字の長さを抽出したいと考えています。

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

main(void)
{
int M,N;

struct word_pair /*structure  */
    {
    char word[M]; // the alphanumeric
    int lenght; //the lenght of alphanumeric
    };
struct word_pair word_table[N]; // the table of alphanumeric and lenght

printf("Enter a frase:");
 scanf("%c",&word_table[N].word[M]);
 printf("\n Your frase is %c ",word_table[N].word[M]);
printf("\n %u ",(unsigned)strlen(word_table[N].word[M]));
}
4

1 に答える 1

0

of の配列を宣言してword_pairいますが、配列内で 1 つしか使用しておらずstruct、それを無効な position( N) に格納しています。配列内の有効な位置nなどの長さの配列を宣言するときは、からからまでであることを考慮してください。int a[n];0n - 1

最初に変数を必要な値に初期化するN必要Mがあります。その後、試してみてください(それと仮定しN > 0M > 0):

scanf("%c",&word_table[0].word[0]);
printf("\n Your frase is %c ",word_table[0].word[0]);
printf("\n %u ",(unsigned)strlen(word_table[0].word[0]));
于 2013-03-04T20:52:43.087 に答える