函数指针
问下哦,怎么才能让函数知道他返回的是一个结构体数组的指针,而不是一个结构体指针,怎么声明 问题点数:20、回复次数:3Top
1 楼tsunamiTom(tom)回复于 2006-03-11 01:37:28 得分 0
代码这。。。实在不知道怎么做了。。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct customer
{
char name[30];
char address[50];
int telephone;
};
typedef struct customer cus;
cus customers[20];
cus customers1[20];
void addsubscriber();
cus * namesequence(cus *cust,int i);
void main()
{
// cus customers[20];
// cus temp; //排序交换用
int choice;
// int i;
while(choice!=7)
{
printf("\t\tTELEPHONE DIRECTORY SYSTEM\n1. Add new subscriber details\n2. Modify existing subscriber details\n3. Delete existing subscriber details\n4. Display subscriber details based on telephone number\n5. Display subscriber details based on subscriber name\n6. View Directory\n7. Quit\n\n");
printf("Enter choice:");
scanf("%d",&choice);
fflush(stdin);
if(choice==1)
addsubscriber();
}
}
void addsubscriber()
{
int count=0;//计算有几条记录
int k;
FILE *fp1;
char flag='Y';
cus newcus;
cus *custom;
int i=0;
while(flag=='Y')
{
for(;;) //读入名字
{
printf("输入顾客名字<最多30个字符>:");
gets(newcus.name);
if((int)strlen(newcus.name)<30&&(int)strlen(newcus.name)!=0)
break;
printf("输入错误用户名不能为空或者超过30个字符请重新输入\n");
}
for(;;) //读入地址
{
printf("请输入地址<最多50个字符>:");
gets(newcus.address);
if((int)strlen(newcus.address)<50&&(int)strlen(newcus.address)!=0)
break;
printf("输入错误,请从新输入,地址不能为空或者不应该超过50个字符\n");
}
for(;;) //读入电话
{
printf("请输入电话:");
scanf("\n%d",&newcus.telephone);
fflush(stdin);
if(!(newcus.telephone<1000||newcus.telephone>9999))
break;
printf("输入错误,请从新输入,电话应该在1000跟9999之间\n");
}
printf("是否需要重新输入按'y'为是'n'为否");
//flag=getchar();
scanf("%c",&flag);
fflush(stdin);
if(flag=='y')
flag=flag-32;
}
//flag='n';
fp1=fopen("telefon.dat","ab"); //写入文件
if(fp1==NULL)
{
fclose(fp1);
printf("没有文件,是否要重新创建文件并追加记录,'y'为是'n'为否");
scanf("%c",&flag);
fflush(stdin);
if(flag=='y')
flag=flag-32;
//printf("%c",)
if(flag=='Y')
{
fp1=fopen("telefon.dat","w+b");
}
else
{
printf("文件不存在,返回操作\n");
return;
}
}
rewind(fp1);
i=0;
fseek(fp1,0,2);
fwrite(&newcus,sizeof(cus),1,fp1);
fclose(fp1);
fp1=fopen("telefon.dat","rb");
while(!feof(fp1))
{
fread(&customers[i],sizeof(cus),1,fp1);
i++;
}
k=0;
customers=namesequence(customers, i);
//while(k<=i)
rewind(fp1);
while(fread(&customers[k],sizeof(cus),1,fp1)==1)
{
printf("%s\t%s\t%d\n",customers[k].name,customers[k].address,customers[k].telephone);
k++;
}
fclose(fp1);
/*
fread(&customers[i],sizeof(cus),1,fp1);
while(!feof(fp1))
{
fread(&customers[i],sizeof(cus),1,fp1);
i++;
}
count=i;
for(k=0;k<=i;k++)
{
if(newcus.name<customers[k].name)
{
while(i>k)
{
customers[i+1]=customers[i];
i--;
}
customers[i]=newcus;
break;
}
}
count++;
rewind(fp1);
for(i=0;i<count;i++)
{
printf("%s\t%s\t%d\n",customers[i].name,customers[i].address,customers[i].telephone);
fwrite(&customers[i],sizeof(cus),1,fp1);
}
*/
}
cus * namesequence(cus cust[],int i)
{
int j;
int k;
cus temp;
for(j=i-2;j>=0;j--)
{
for(k=i-1;k>=i-j;k--)
{
if(cust[k].name>cust[k-1].name)
{
temp=cust[k];
cust[k]=cust[k-1];
cust[k-1]=temp;
}
}
}
return(cust);
}
Top
2 楼jixingzhong(瞌睡虫·星辰)回复于 2006-03-11 11:47:56 得分 20
函数中参数增加一个,指明数组维数。
如果只是单一的结构体,数组维数为 1 就可以了 ~Top
3 楼tsunamiTom(tom)回复于 2006-03-11 12:48:54 得分 0
现在是返回值有问题。。Top




