Thanks for any help. Most of this works, but I get a compiler error on lines 60, 84, and 105, where it says

current = currentStore->link

The error reads "assignment from incompatible pointer type" It runs, but in the printStores function when i gets incremented on line 81 it changes from 0 to 3 (debug watch) instead of 1 and then it only prints the first store and exits the for loop. I'd appreciate any help I can get. I already asked my teacher and she told me it looks right. I've tried on 1 linux and two windows computers with the same result.

#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(LINKEDLIST));
    list->head = NULL;
    list->count = 0;
    return list;
}
/* ************************************************************************************ */
STORE *createVacantStore()
{
    STORE *newVacantStore = malloc(sizeof(STORE));
    storeCount++;
    newVacantStore->storeNumber = storeCount;
    strcpy(newVacantStore->storeName, "vacant");
    strcpy(newVacantStore->phoneNumber, "000-0000");
    newVacantStore->link = NULL;
    return newVacantStore;
}
/* void printList(VACANTLIST *list); */
/* ********************************************************************************************** */
STORE *addVacantStore(LINKEDLIST *list)
{
    STORE *currentStore;
    STORE *store;
    store = createVacantStore();
    if (list->head == NULL)
    {
        list->head = store;
    }
    else
    {
        currentStore = list->head;
        char *test = currentStore->storeName;
        printf("%s\n\n\n", test);
        while (currentStore != NULL)
        {
            currentStore = currentStore->link;
        }
        currentStore = store;

    }
    list->count++;
    return currentStore;
}
/* ****************************************************************************************** */
void printStores(LINKEDLIST *list)
{
    int i = 0;
    STORE *currentStore;
    int j = list->count;

    if (list->head == NULL)
    {
        printf("The list is empty");
    }
      currentStore = list->head;
      //printf("%s", currentStore->storeName);
      for(i = 0; i < j; i++);
      {
      printf("%d\t%s\t%s\n", currentStore->storeNumber, currentStore->storeName, currentStore->phoneNumber);
       currentStore = currentStore->link;

        }
    }



/* **************************************************************************** */
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 != NULL && currentStore->storeNumber != searchTarget)
        {
            currentStore = currentStore->link;
        }

        if (currentStore->storeNumber == 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;
}

Recommended Answers

All 4 Replies

The problem is (I think) in how you define the structure

typedef struct
{
    int storeNumber;
    char storeName[STORE_NAME_SIZE];
    char phoneNumber[STORE_PHONE_SIZE];
    struct STORE *link;
}STORE;

at line 6, you have no type struct STORE. You have a defined type STORE and an unnamed structure.

if you did this

typedef struct store_t
{
    int storeNumber;
    char storeName[STORE_NAME_SIZE];
    char phoneNumber[STORE_PHONE_SIZE];
    struct STORE *link;
}STORE;

Then you would have a defined type STORE and a structure named store_t or type struct store_t then

struct store_t* ptr1;
STORE* ptr2;

would be equivilent.

Thanks for the help, I see what you mean I think, but when I change it to the following I still get the same errors.

#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 store_t
{
    int storeNumber;
    char storeName[STORE_NAME_SIZE];
    char phoneNumber[STORE_PHONE_SIZE];
    struct STORE *link;
}STORE;


typedef struct list_t
{
  STORE *head;
    int count;
}LINKEDLIST;
/* ************************************************************************************ */
LINKEDLIST *createList()
{
    LINKEDLIST *list = malloc(sizeof(LINKEDLIST));
    list->head = NULL;
    list->count = 0;
    return list;
}
/* ************************************************************************************ */
STORE *createVacantStore()
{
    STORE *newVacantStore = malloc(sizeof(STORE));
    storeCount++;
    newVacantStore->storeNumber = storeCount;
    strcpy(newVacantStore->storeName, "vacant");
    strcpy(newVacantStore->phoneNumber, "000-0000");
    newVacantStore->link = NULL;
    return newVacantStore;
}
/* void printList(VACANTLIST *list); */
/* ********************************************************************************************** */
STORE *addVacantStore(LINKEDLIST *list)
{
    STORE *currentStore;
    STORE *store;
    store = createVacantStore();
    if (list->head == NULL)
    {
        list->head = store;
    }
    else
    {
        currentStore = list->head;
        char *test = currentStore->storeName;
        printf("%s\n\n\n", test);
        while (currentStore != NULL)
        {
            currentStore = currentStore->link;
        }
        currentStore = store;

    }
    list->count++;
    return currentStore;
}
/* ****************************************************************************************** */
void printStores(LINKEDLIST *list)
{
    int i = 0;
    STORE *currentStore;
    int j = list->count;

    if (list->head == NULL)
    {
        printf("The list is empty");
    }
      currentStore = list->head;
      //printf("%s", currentStore->storeName);
      for(i = 0; i < j; i++);
      {
      printf("%d\t%s\t%s\n", currentStore->storeNumber, currentStore->storeName, currentStore->phoneNumber);
       currentStore = currentStore->link;

        }
    }



/* **************************************************************************** */
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 != NULL && currentStore->storeNumber != searchTarget)
        {
            currentStore = currentStore->link;
        }

        if (currentStore->storeNumber == 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;
}

No you need to alter line 14 of your code listing, it still does not refer to a defined type.

Thank you for the help, I was able to fix the compiler errors, but was still having trouble getting my link (next) set right when adding to the end of the list, but I finally figured it out. Thanks again!

#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 store_type
{
    int storeNumber;
    char storeName[STORE_NAME_SIZE];
    char phoneNumber[STORE_PHONE_SIZE];
    struct store_type *link;
}STORE;


typedef struct
{
  STORE *head;
    int count;
}LINKEDLIST;
/* ************************************************************************************ */
LINKEDLIST *createList()
{
    LINKEDLIST *list = malloc(sizeof(LINKEDLIST));
    list->head = NULL;
    list->count = 0;
    return list;
}
/* ************************************************************************************ */
STORE *createVacantStore()
{
    STORE *newVacantStore = malloc(sizeof(STORE));
    storeCount++;
    newVacantStore->storeNumber = storeCount;
    strcpy(newVacantStore->storeName, "vacant");
    strcpy(newVacantStore->phoneNumber, "000-0000");
    newVacantStore->link = NULL;
    return newVacantStore;
}
/* void printList(VACANTLIST *list); */
/* ********************************************************************************************** */
STORE *addVacantStore(LINKEDLIST *list)
{
    STORE *currentStore = NULL;
    STORE *store = NULL;
    store = createVacantStore();
    if (list->head == NULL)
    {
        list->head = store;
    }
    else
    {
        currentStore = list->head;


        while(currentStore->link != NULL)
        {
            currentStore = currentStore->link;

        }
        currentStore->link = store;

    }

    list->count++;
    return currentStore;
}
/* ****************************************************************************************** */
void printStores(LINKEDLIST *list)
{
    int i = 0;
    STORE *currentStore;

    if (list->head == NULL)
    {
        printf("The list is empty");
    }
      currentStore = list->head;


      for( i = 0; i < list->count; i++)
      {
      printf("%d\t%s\t%s\n", currentStore->storeNumber, currentStore->storeName, currentStore->phoneNumber);
       currentStore = currentStore->link;

        }
    }



/* **************************************************************************** */
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 != NULL && currentStore->storeNumber != searchTarget)
        {
            currentStore = currentStore->link;
        }

        if (currentStore->storeNumber == 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;
}
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.