马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define DSIZE 1024
struct Person
{
char name[40];
char phone[20];
};
int len=0;
struct Contacts
{
struct Person person[DSIZE];
}contacts;
void getinput(struct Person *person)
{
printf("输入名字\n");
scanf("%s",person->name);
printf("输入号码\n");
scanf("%s",person->phone);
}
void addperson()
{
struct Person *person;
if(len >= DSIZE)
{
fprintf(stderr,"内存已满\n");
exit(1);
}
getinput(&person[len]);
len++;
}
void displayperson(struct Person person)
{
printf("姓名:%s",person.name);
printf("电话:%s",person.phone);
}
void displaycontacts()
{
int i;
for(i=0;i<len;i++)
{
displayperson(&contacts[i]);
putchar('\n');
}
}
void releasecontacts()
{
free(&contacts);
}
int main(void)
{
int ch;
struct Person* person=NULL;
contacts = (struct Contacts *)malloc(DSIZE*sizeof(*person));
if(contacts == NULL)
{
fprintf(stderr,"内存分配失败\n");
exit(1);
}
while(1)
{
printf("是否录入");
do
{
ch = getchar();
}while(ch!='y'&& ch!='n');
if(ch == 'y')
{
addperson();
}
else if(ch == 'n')
break;
}
displaycontacts();
releasecontacts();
exit(0);
}
如何修改以上程序,使其能正常运行 |