让代码更简单

多层结构体指针使用出错的问题

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct Customer{
char number[10];
char name[10];
int save_money;
int kind;
int time;
int year[100];
int month[100];
int day[100];//和time结合表示第几次送水的日期;
int is_member;//0和1
struct Customer* next;
}customer,*pcustomer;
//顾客的基本信息包括:
typedef struct Information{
int money;//公司资金
int water1_number;
int water2_number;
int water3_number;//公司水的库存

}information;

typedef struct company{
pcustomer data1;
information data2;
}Company,*pcompany;
void write_in(pcustomer acustomer){
int judge;
int m;
int ls;
printf("请输入客户的编号");
scanf("%s",&acustomer->number);
printf("请输入客户的姓名:");
scanf("%s",&acustomer->name);
printf("请输入客户订水的种类:");
scanf("%d",&acustomer->kind);//待改进
printf("该客户是否为会员?");
scanf("%d",&judge);

}
pcustomer cream_customer(){
pcustomer acustomer;
acustomer=(pcustomer)malloc(sizeof(customer));
acustomer->time=0;//刚刚建立的客户信息的初始化
acustomer->save_money=-1;
acustomer->next=NULL;
return acustomer;
} //创建一个新的客户信息
void insert(pcompany water_delivery){
// pcustomer new_customer=cream_customer();
// write_in(new_customer);
pcustomer acustomer=(pcustomer)malloc(sizeof(customer));
acustomer->time=0;//刚刚建立的客户信息的初始化
acustomer->save_money=-1;
acustomer->next=NULL;
int judge;
int m;
int ls;
printf("请输入客户的编号");
scanf("%s",&acustomer->number);
printf("请输入客户的姓名:");
scanf("%s",&acustomer->name);
printf("请输入客户订水的种类:");
scanf("%d",&acustomer->kind);//待改进
printf("该客户是否为会员?");
scanf("%d",&judge);
pcustomer p=water_delivery->data1->next;
while(p){
if(strcmp(acustomer->number,p->number)==0){
printf("系统中存在相同编号的客户,可能输入错误!!!");
system("pause");
return;
}
p=p->next;
}
acustomer->next=water_delivery->data1->next;
water_delivery->data1->next=acustomer;
}

void test(pcompany water_delivery){
printf("%d",water_delivery->data1);
}
void main(){
pcompany water_delivery=(pcompany)malloc(sizeof(pcompany));
water_delivery->data1=(pcustomer)malloc(sizeof(pcustomer));
water_delivery->data1=cream_customer();
insert(water_delivery);
insert(water_delivery);
test(water_delivery);
}

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/94023227543610.png "=600 #left")
怎样使用这个结构体的指针,怎样才能避免指针使用出错?以及正确的使用方法 (**可能出错的对方insert中,以及各个结构体的初始化**)
1638217534qq-com 2021-10-22 提问 0人浏览
分享
写回答

共以下 0 个回答:

写回答:

问题:多层结构体指针使用出错的问题

  • 插入代码
登陆后回答
QQ咨询 邮件咨询 狗哥推荐