私は10人のレーサーから情報を取得するプログラムを作成しようとしています。プログラムは、名、姓、年齢、性別(m/f)、レースの時間(hh:mm:ss)を取得して保存します。これを行うために、私は各レーサーのための上記の要素のそれぞれを含む構造の配列を持つことを計画しました。 「最初のレーサーの名前を入力してください」という質問は、「最初の」という単語を「2番目の」、「3番目の」などに変更する必要があるためです。ループはそれを行う。だから私は、配列の最初の要素が "first"というような文字列を作ることにしました。それで、私はplace配列の各要素にアクセスすることによって、各レーサーの正しい単語を印刷するループを使用することができました。文字列の配列とそれらの要素の印刷
私は文字列や文字列の配列に慣れていないので、私はオンラインでいくつかのヘルプを検索し、次のプログラムを思いつきました。ポインタを持つ文字配列を使用しています。文字列とは何かとにかく、私はプログラムを実行するときに深刻な問題が発生し、Visual Studioを再起動する必要があります。誰かが私に手を差し伸べて、これらの文字列の配列とポインタの意味についてのいくつかの謎を解明するのに役立ちます。ありがとう!
#include <stdio.h>
#include <math.h>
typedef struct DATASET
{
char firstname[12], lastname[12], gender;
int age, hours, minutes, seconds;
};
#define MaxRacers 10
int main()
{
int i;
DATASET data[MaxRacers];
char *places[MaxRacers];
char place1[6] = "First";
char place2[7] = "Second";
char place3[6] = "Third";
char place4[7] = "Fourth";
char place5[6] = "Fifth";
char place6[6] = "Sixth";
char place7[8] = "Seventh";
char place8[7] = "Eighth";
char place9[6] = "Ninth";
char place10[6] = "Tenth";
places[0] = place1;
places[1] = place2;
places[2] = place3;
places[3] = place4;
places[4] - place5;
places[5] = place6;
places[6] = place7;
places[7] = place8;
places[8] = place9;
places[9] = place10;
printf("%s", places[1]); // TEST which works fine
for(i = 0, i < MaxRacers; i = i + 1;)
{
printf("Enter the name of the %s finisher\n", places[i]); // Problem
}
getchar();
return(0);
}
今、アイブ氏は、イムは、今のようにすぐに私は最初のフィニッシャープログラムは、コマンドウィンドウの外に出て、新しいウィンドウが立ち上がるの最後の名前の入力が完了して問題に実行して、さらに少し行く事を得ました言って:
「ConsoleApplication30.exeで0x0FF6D0F1(ucrtbased.dll)でスローされる例外:0xc0000005で:。。アクセス違反の書き込み場所0xFFFFFFCC
この例外のハンドラがある場合、プログラムは安全に継続することができる」
#include <stdio.h>
#include <math.h>
struct DATASET
{
char firstname[12], lastname[12], gender;
int age, hours, minutes, seconds;
};
#define MaxRacers 10
int main()
{
int i;
DATASET data[MaxRacers];
char *places[] = { "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth" };
for (i = 0; i < MaxRacers; i++)
{
printf("Enter the first name of the %s finisher:\n", places[i]);
scanf("%s", data[i].firstname);
printf("Enter the last name of the %s finisher:\n", places[i]);
scanf("%s", data[i].lastname);
printf("Enter the gender of the %s finisher: [m/f]: \n", places[i]);
scanf("%c", data[i].gender);
printf("Enter the age of the %s finisher:\n", places[i]);
scanf("%d", data[i].age);
printf("Enter the time of the %s finisher: [hh:mm:ss]\n", places[i]);
scanf("%d:%d:%d", data[i].hours, data[i].minutes, data[i].seconds);
printf("\n\n");
}
getchar();
return(0);
}
'places [4] - place5;はtypoです。コンパイラはそれについてあなたに警告していたでしょう。 – Evert
'char * place1 =" First "を使う方がおそらくもっと標準です。 – Evert
または、すべての行を割り当てます。 'char * places [] = {"最初 "、" 2番目 "、" 3番目 "、...};'。それははるかに明確になるでしょう。 – Evert