Hi all. I've got the task of making a car rental program, which uses linked lists to control what cars are available for rent, are rented, or are in repair. The 'car' is a structure, and so is the rented list, available list, and repair list.
Heres my current issue. If the user wants to make a new car available, we must add it to our list of all possible cars, and we must add it to the list of available cars.
I have no problem adding it to the list of cars, but when i need to add it to the list of available cars, i get a segmentation fault.
I will now provide the code:
typedef struct vehicles
{
char idNum[20];
int miles;
int rDate;
struct vehicles *nextCar;
}car;
typedef struct list
{
car * aCar;
struct list *nextCar;
} carList;
The list of all cars is:
car * carHead, * carCur;
The list of all available cars is:
carList * availHead, * availCur;
Both are initialized to NULL.
I then create a new car, and put in the data the user has given me (mileage and ID number)
carCur = (car *)malloc(sizeof(car));
//set ID, Mileage
for(k=0;k<=19;k++)
{
carCur->idNum[k] = idNum[k];
}
carCur->miles = miles;
carCur->nextCar = NULL;
This works perfectly fine. I call the function which actually adds it to the list, all is well.
Then i create a new carList structure to be added to the available cars list.
availCur = (carList *)malloc(sizeof(carList));
//set ID, Mileage
for(k=0;k<=19;k++)
{
availCur->aCar->idNum[k] = idNum[k];
printf("assigned\n");
}
availCur->aCar->miles = miles;
availCur->nextCar = NULL;
After some testing using printf statements,(which werent all included here for brevity), i found the seg fault occurs in this statement.
availCur->aCar->idNum[k] = idNum[k];
I'm hoping someone can tell me why this assignment results in a segfault. I've checked the idNum provided by the user is good, and it works for adding to the all-cars list, so I'm not sure what is wrong.
I appreciate the help!