は、以下のstruct
例外関数内で配列をreallocをしようとしている
struct HotelManagement
{
Hotel_t *hotel;
Customer_t *customers;
reservation_t *reservations;
int physicalSize;
int registerdSize;
int physicalSizeReserv;
int registerdSizeReserv;
} typedef HotelManagement_t;
そして次main
を付けながら:
printf("-----DETAILS FOR CUSTOMER-----\n");
printf("enter name ");
gets(tempName);//"cleans" the buffer
gets(tempName);
customerName = strdup(tempName);
printf("nenter credit card ");
gets(tempCredit);
creditCardNumber = strdup(tempCredit);
printf("enter credit card expiration month(mm)\n");
printf("enter credit card expiration year(yyyy)\n");
scanf("%d",&month);
scanf("%d",&year);
addCustomer(customerName,creditCardNumber, month,year,&hm);
とaddCustomer
の次の実装:
void addCustomer(char *customerName, char *numberOfCreditCard,int month,int year,HotelManagement_t *hotelMang)
{
int *m = &month;
int *y = &year;
int i;
if (hotelMang->physicalSize == hotelMang->registerdSize)
{
hotelMang->customers = (Customer_t*)realloc(hotelMang->customers, hotelMang->registerdSize * sizeof(Customer_t));
}
hotelMang->customers[hotelMang->registerdSize].id = hotelMang->registerdSize+1;//id starts with '1'
hotelMang->customers[hotelMang->registerdSize].cName = (char*)malloc(strlen(customerName)*sizeof(char));//initalize space for the Customers name
strcpy(hotelMang->customers[hotelMang->registerdSize].cName ,customerName);
if (checkValidCreditCard(&month,&year,numberOfCreditCard) == 1)
{
hotelMang->customers[hotelMang->registerdSize].credit = (char*)malloc(strlen(numberOfCreditCard)*sizeof(char));//initalize size
strcpy(hotelMang->customers[hotelMang->registerdSize].credit, numberOfCreditCard);
hotelMang->customers[hotelMang->registerdSize].month = month;
hotelMang->customers[hotelMang->registerdSize].year = year;
}
//increments
for (i = 0; i <= hotelMang->registerdSize; i++)
{
printOneCustomer(hotelMang->customers[i]);
}
hotelMang->registerdSize= hotelMang->registerdSize +1;
}
theresの配列を初期化する関数...
hotelManagement->customers = (Customer_t*)calloc(1, sizeof(Customer_t));
hotelManagement->hotel->roomsMat = (Room_t**)calloc(floors, sizeof(Room_t*));
hotelManagement->reservations = (reservation_t*)calloc(1, sizeof(reservation_t));
私は顧客にスペースを再割り当てしたいのですが、私は顧客に配列を追加していますが、例外がスローされます。その理由は何でしょうか?
私の***推測***? 'HotelManagement_t'構造体' hm'は初期化しません。 –
また、決して*決して*使用することはありません!これは危険な機能であり、最新のC標準からも削除されています。例えば['fgets'](http://en.cppreference.com/w/c/io/fgets)を参照してください。 –
それは全体がメインですか?はいの場合は、3つの欠落線を追加します。そうでなければ、あなたは何かを逃したのだろうかと思います – klutt