Cのコードに問題があります。 Enterボタンで区切られた名前のリストを入力する必要があります。 ユーザーが「QUIT」という単語を入力すると入力が停止します。 プログラムは名前のアルファベット順のリストを出力する必要があります(すべての文字は小文字です)。ポインタの配列の動的割り当て
名前の数と各名前の長さは不明であり、動的に割り当てられる必要があります。また、名前が入力に2回以上出現する場合は、出力に1回だけ表示されます。ここ
は、コードを実行する方法のための例です:
Please enter a list of names:
john
chris
ben
chris
david
QUIT
4名があります。
ben
chris
david
john
私は、各ポインタは、それぞれが含まれているポインタの動的に割り当てられた配列を、使用して考えたが、ダイナミックに割り当てられています。問題は、実行時エラーを出さずに書き込む方法がわからないことです。
注:現時点では、構造体や再帰のようにまだ学習していないものを使用することはできません。ライブラリstdio.h、stdlib.h、およびstring.hのみを使用できます。
ありがとうございます。
ここのコード(それは完全ではないのですが、私はこの時点でランタイムエラーを取得しています)です:
char **nameList;
int i = 0, j = 0, size = 0, check = 0;
printf("Please enter list of names:\n");
//allocate one cell of memory to the list
nameList = (char**)malloc(sizeof(char));
if (nameList == NULL)
{
printf("Cannot allocate Memory");
return 0;
}
//Add the first letter to the first string in the array
nameList[i][j] = getchar();
size += sizeof(char);
while (check != 1)
{
//check if current entered letter is not an enter
while (nameList[i][j] != '\n')
{
//allocated another char sized memory to the string
nameList = (char**)realloc(nameList, (size + sizeof(char)));
if (nameList == NULL)
{
printf("Cannot allocate Memory");
return 0;
}
j++;
//adding another char to the current string
nameList[i][j] = getchar();
size += sizeof(char);
}
j = 0;
if (nameList[i][j] == 'Q')
{
if (nameList[i][j + 1] == 'U')
if (nameList[i][j + 2] == 'I')
if (nameList[i][j + 3] == 'T')
check++;
}
i++;
}
[ask]と[mcve]を読んでください。あなたはランタイムエラーについて言及していますが、それを引き起こすコードは何ですか?修正しようとした特定の問題を解決するのに役立ちますが、スタックオーバーフローはコード作成サービスではありません。 – whrrgarbl
これで、ポインタと文字列の配列を動的に割り当てることができるはずです。だからあなたは現在のコードとあなたのエラーを表示する必要があります。 –
私は、この生徒が学ぶチャンスを奪った答えを投票します。 –