(C) 从调用其他函数的函数打印结构数组
我为一个程序编写了一段代码,该程序要求用户填写关于一个人的信息((C) 从调用其他函数的函数打印结构数组,c,pointers,struct,C,Pointers,Struct,我为一个程序编写了一段代码,该程序要求用户填写关于一个人的信息(ReadDate/ReadPerson),然后它要求用户打印的人的范围,然后该函数打印这些人。(打印范围) 我不明白为什么程序不起作用;它被卡在了应该打印人物的地方。。。这意味着PrintPerson或PrintDate中可能有问题,或者我调用这些函数的方式有问题 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <string.h>
ReadDate
/ReadPerson
),然后它要求用户打印的人的范围,然后该函数打印这些人。(打印范围
)
我不明白为什么程序不起作用;它被卡在了应该打印人物的地方。。。这意味着PrintPerson
或PrintDate
中可能有问题,或者我调用这些函数的方式有问题
这是我的密码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int day, month, year;
} Date;
typedef struct{
char *first_name, *last_name;
int id;
Date birthday;
} Person;
void ReadDate(Date *a)
{
printf("Enter the day: ");
scanf("%d", &a->day);
printf("Enter the month (1-12): ");
scanf("%d", &a->month);
printf("Enter the year: ");
scanf("%d", &a->year);
}
void ReadPerson(Person *b)
{
char temp_first_name[21];
char temp_last_name[21];
printf("Enter the first name: ");
gets(temp_first_name);
b->first_name = (char*)malloc(strlen(temp_first_name)+1);
strcpy(b->first_name,temp_first_name);
//need to check malloc (later)
printf("Enter the last name: ");
gets(temp_last_name);
b->last_name = (char*)malloc(strlen(temp_last_name)+1);
strcpy(b->last_name, temp_last_name);
//need to check malloc (later)
printf("Enter the id number: ");
scanf("%d",&b->id);
printf("Enter the birthday:\n");
ReadDate(&b->birthday);
}
int ReadAllDate (Person *b)
{
//Person* b;
int count=1;
char choice;
printf("Would you like to enter a person?(y,n)\n");
choice = getchar();
while(choice == 'y')
{
b = (Person*)malloc(1 * sizeof(Person));
getchar();
ReadPerson(b);
printf("Done!\n");
count++;
getchar();
printf("Would you like to add a person?(y,n)\n");
choice = getchar();
}
count--;
printf("The number of entered persons is %d\nBye\n", count);
return count;
}
void PrintPerson(Person b)
{
printf("%s %s %d\n", b.first_name, b.last_name, b.id);
}
void PrintDate(Date a)
{
printf("%d // %d // %d\n",a.day,a.month,a.year);
}
void PrintRange(Person *b,int count,int ind1,int ind2)
{
int i;
if (ind1<0 || ind1>ind2 || ind2>count)
{
printf("error! you slip out from the limits of the array.\n");
}
else
{
for (i=ind1; i<=ind2; i++)
{
PrintPerson(*(b+i));
PrintDate((b+i)->birthday);
}
}
}
int main(int argc, const char * argv[])
{
Person* b;
int count;
int ind1, ind2;
count = ReadAllDate(b);
printf("insert the first index (the smaller): ");
scanf("%d", &ind1);
printf("insert the second index (the bigger): ");
scanf("%d", &ind2);
PrintRange(b, count, ind1, ind2);
}
#包括
#包括
#包括
类型定义结构{
整数日、月、年;
}日期;
类型定义结构{
字符*名,*姓;
int-id;
生日;
}人;
无效读取日期(日期*a)
{
printf(“输入日期:”);
scanf(“%d”,&a->day);
printf(“输入月份(1-12):”;
scanf(“%d”、&a->month);
printf(“输入年份:”);
scanf(“%d”、&a->year);
}
无效读写人(人*b)
{
char temp_first_name[21];
char temp_last_name[21];
printf(“输入名字:”);
获取(临时名称);
b->first_name=(char*)malloc(strlen(temp_first_name)+1);
strcpy(b->first\u name,temp\u first\u name);
//需要检查malloc(稍后)
printf(“输入姓氏:”);
获取(临时姓氏);
b->last_name=(char*)malloc(strlen(temp_last_name)+1);
strcpy(b->last\u name,temp\u last\u name);
//需要检查malloc(稍后)
printf(“输入id号:”);
scanf(“%d”和&b->id);
printf(“输入生日:\n”);
读取日期(&b->生日);
}
int ReadAllDate(个人*b)
{
//个人*b;
整数计数=1;
字符选择;
printf(“您想输入一个人吗?(y,n)\n”);
choice=getchar();
while(选项='y')
{
b=(人*)马洛克(1*sizeof(人));
getchar();
读者(b);
printf(“完成!\n”);
计数++;
getchar();
printf(“您想添加一个人吗?(y,n)\n”);
choice=getchar();
}
计数--;
printf(“输入的人数为%d\n字节\n”,计数);
返回计数;
}
无效打印人(b人)
{
printf(“%s%s%d\n”,b.名字,b.姓氏,b.id);
}
作废打印日期(日期a)
{
printf(“%d//%d/%d\n”,a.day,a.month,a.year);
}
无效打印范围(个人*b、整数计数、整数ind1、整数ind2)
{
int i;
如果(ind1ind2 | | ind2>计数)
{
printf(“错误!您滑出了数组的限制。\n”);
}
其他的
{
对于(i=ind1;i=d);
}
}
}
int main(int argc,const char*argv[]
{
个人*b;
整数计数;
int ind1,ind2;
计数=读取日期(b);
printf(“插入第一个索引(较小者):”;
scanf(“%d”和&ind1);
printf(“插入第二个索引(较大者):”;
scanf(“%d”和&ind2);
打印范围(b、计数、ind1、ind2);
}
问题在于,实际上在任何地方都没有创建Person
对象数组。函数ReadAllDate()
从不更改main()
中变量b
的值。相反,它为您读入的每个人分配内存,并将指向该person
的指针存储在ReadAllDate()中的局部变量b
中。每次执行此操作时,它都会忘记以前的b
在main()
中,b
的值一直是单位化的。当您试图打印人员时,它很可能会崩溃或打印垃圾
我发现您的代码存在其他问题:
- 不要使用
gets()
,始终使用fgets()
。前者可以写入超过您提供的缓冲区的末尾
- 不要做malloc(strlen(…)
+strcpy()
,使用strdup()
count=0开始,这样您就不需要ReadAllDate()中的计数--
。作为一名计算机程序员,最好从零开始计数,而不是从一开始
b[i]
而不是*(b+i)
,写b[i]。生日而不是(b+i)->生日
问题在于,在任何地方,实际上都没有创建
Person
对象数组。函数ReadAllDate()
从不更改main()
中变量b
的值。相反,它为您读入的每个人分配内存,并将指向该person
的指针存储在ReadAllDate()中的局部变量b
中。每次执行此操作时,它都会忘记以前的b
在main()
中,b
的值一直是单位化的。当您试图打印人员时,它很可能会崩溃或打印垃圾
我发现您的代码存在其他问题:
- 不要使用
gets()
,始终使用fgets()
。前者可以写入超过您提供的缓冲区的末尾
- 不要做malloc(strlen(…)
+strcpy()
,使用strdup()
count=0开始,这样您就不需要ReadAllDate()中的计数--
。作为一名计算机程序员,最好从零开始计数,而不是从一开始
b[i]
而不是*(b+i)
,写b[i]。生日而不是(b+i)->生日
您能否提供发生的错误?“EXC\u Bad\u Access…code…”在Xcode上。(mac)。。它在你的电脑上为你工作?总是,总是验证所有用户输入(例如,
if(scanf(“%d”)和&a->day)!=1{/*handle error*/}
至少。你能提供发生的错误吗?“EXC\u Bad\u Access…code…”它在Xcode上。(mac)…它在你的电脑上为你工作吗?总是,总是验证所有用户输入(例如,if(scanf(%d“,&a->day)!=1){/*handleerror*/}
最小值。还有mem