Hello, and thanks in advance for any help I can get. This is part of a larger program for homework. Things were working when I called "addVacantStore" the first time before I wrote the search function. I then called it twice more to set up to test the search function. On the second of the three times it gets to "STORE *newVacantStore = malloc(sizeof(newVacantStore));" line in the createVacantStore function and gives a seg fault error, and I can't figure out why. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STORE_NAME_SIZE 35
#define STORE_PHONE_SIZE 8
int storeCount = 0;
int searchTarget = 0;
typedef struct
{
int storeNumber;
char storeName[STORE_NAME_SIZE];
char phoneNumber[STORE_PHONE_SIZE];
struct STORE* link;
}STORE;
typedef struct
{
STORE *head;
int count;
}LINKEDLIST;
/* ************************************************************************************ */
LINKEDLIST *createList()
{
LINKEDLIST *list = malloc(sizeof(list));
list->head = NULL;
list->count = 0;
return list;
}
/* ************************************************************************************ */
STORE *createVacantStore()
{
STORE *newVacantStore = malloc(sizeof(newVacantStore));
storeCount++;
newVacantStore->storeNumber = storeCount;
strcpy(newVacantStore->storeName, "vacant");
strcpy(newVacantStore->phoneNumber, "000-0000");
return newVacantStore;
}
/* ********************************************************************************************** */
void *addVacantStore(LINKEDLIST *list)
{
STORE *current;
STORE *store;
store = createVacantStore();
if (list->head == NULL)
{
list->head = store;
}
else
{
while (current->link != NULL)
{
current = current->link;
}
current->link = store;
list->count ++;
}
}
/* ****************************************************************************************** */
void printStores(LINKEDLIST *list)
{
STORE *currentStore;
if (list->head != NULL)
{
for (currentStore = list->head; currentStore != NULL; currentStore = currentStore->link)
{
printf("%d\t%s\t%s\n", currentStore->storeNumber, currentStore->storeName, currentStore->phoneNumber);
}
}
else
{
printf("The list is empty");
}
}
/* **************************************************************************** */
void printIndividualStore(STORE *currentStore)
{
printf("%d\t%s\t%s\n", currentStore->storeNumber, currentStore->storeName, currentStore->phoneNumber);
}
/* **************************************************************************** */
STORE *searchStore(int searchTarget, LINKEDLIST *list)
{
STORE *currentStore;
if (list->head != NULL)
{
currentStore = list->head;
while (currentStore->link != NULL && currentStore->storeNumber != searchTarget)
{
currentStore = currentStore->link;
}
if (currentStore == searchTarget)
{
return currentStore;
}
}
return NULL;
}
/* **************************************************************************** */
int main()
{
STORE *store;
LINKEDLIST *vacantList;
LINKEDLIST *occupiedList;
vacantList = createList();
occupiedList = createList();
addVacantStore(vacantList);
addVacantStore(vacantList);
addVacantStore(vacantList);
printStores(vacantList);
/* searchTarget = 2;
store = searchStore(searchTarget, vacantList);
printIndividualStore(store); */
return 0;
}