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;
}

Line 55. How current is initialized?

I hadn't noticed that, and added a line, but I still get the same problem at line 35 when calling addVacantStore the second time it gives me a seg fault error. Any ideas?

#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;
    newVacantStore = malloc(sizeof(newVacantStore));
    storeCount++;
    newVacantStore->storeNumber = storeCount;
    strcpy(newVacantStore->storeName, "vacant");
    strcpy(newVacantStore->phoneNumber, "000-0000");
    return newVacantStore;
}
/* void printList(VACANTLIST *list); */
/* ********************************************************************************************** */
void *addVacantStore(LINKEDLIST *list)
{
    STORE *current;
    STORE *store;
    store = createVacantStore();
    if (list->head == NULL)
    {
        list->head = store;
    }
    else
    {
        current = list->head;
        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;
}

Line 26 LINKEDLIST *list = malloc(sizeof(list)); Line 34 - 35

STORE *newVacantStore;
newVacantStore = malloc(sizeof(newVacantStore)); newVacantStore = malloc(sizeof(newVacantStore));

In both of these cases you have allocated the size of the pointer not the size of the object pointed to. Pointer is likely to be 4 bytes on a 32bit system (or 8 on a 64bit system).

When you malloc you need to allocate the size of the thing pointed to not the size of the pointer so line 26 should be LINKEDLIST *list = malloc(sizeof *list); or LINKEDLIST *list = malloc(sizeof(LINKEDLIST)); I leave line 35 for you to correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.