我试图创建一个程序,将从10个赛车手的信息。该程序将获得并存储名字,姓氏,年龄,性别(m/f)和他们的比赛时间(hh:mm:ss)。为此,我计划为每个赛车手提供一个包含上述每个元素的结构数组。然后我遇到了问“请输入第一名赛车手的名字”的问题,因为“first”这个词需要更改为“second”,“third”等......循环将无法去做。于是我决定创建一个字符串数组,其中数组的第一个元素是单词“first”,依此类推。那么我可以通过循环访问places数组的每个元素来为每个赛车手打印正确的单词。字符串阵列,并打印这些元素
我对字符串和字符串数组不熟悉,我在网上搜索了一些帮助,并提出了下面的程序,它使用带指针的字符数组,我不太明白,必须是与弦乐有关。无论如何,当我运行程序时,我遇到了严重的问题,必须重新打开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);
}
现在,香港专业教育学院得到的东西去远一点,进出口运行到一个问题,现在我只要一输入完第一个整理程序退出了命令窗口和一个新的窗口出现的姓氏他说:
“异常在0x0FF6D0F1(ucrtbased.dll)在ConsoleApplication30.exe抛出:0000005:访问冲突写入位置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;'是一个错字。编译器可能会就此提醒你。 – Evert
它可能更标准的使用'char * place1 =“First”; char * place2 =“Second”;'等等 – Evert
或者全部指定:'char * places [] = {“First”,“Second”,“Third”,...};'。这将更清晰。 – Evert