0

I seem to not be able to find the problem to make the program work. C is telling me "error: Invalid operands to binary != 'grocerylist' (aka struct grocerylist) and 'int' When i try to solve this problem other bugs pop out, can anyone see other problems in this code except for the bug I posted about?

#include <stdio.h> 
#include <stdlib.h>
#define MAX 100

typedef struct grocerylist
{
    char name[MAX];
    float ammount;
    char unit[MAX];
} grocerylist;



struct grocerylist * enterItems(int arr)
{
    grocerylist * itemtolist;
    itemtolist = (grocerylist*)malloc(sizeof(grocerylist)*arr);

    if (*itemtolist != 0)
    {
        int i;

        for(i = 0; i < arr; i++)
        {
            printf("Enter item name: /n");
            scanf("%c", (itemtolist[i]).name);
            printf("Enter ammount of item: /n");
            scanf("%f", &(itemtolist[i]).ammount);
            printf("Enter unit of item: /n");
            scanf("%c", (itemtolist[i]).unit);
        }

    }
    return itemtolist;
}


void printShoppingList(grocerylist *itemtolist, int arr)
{
    int i;
    for (i = 0; i < arr; i++)
    {
        printf("%s, %f, %s", itemtolist[i].name, itemtolist[i].ammount, 
               itemtolist[i].unit);
    }
}



int main(void)
{
    int arr, number;
    grocerylist * itemtolist;

    while (number == 0)
    {
        printf("How many items would you like to add to your list? /n");
        scanf("%i", &arr);

        itemtolist = enterItems(arr);
        printShoppingList(itemtolist, arr);
        free(itemtolist);

        printf("Do you want to enter another item. 0 for yes, 1 for no");
        scanf("%i", &number);
    }
    return 0;
}
Wreigh
  • 3,215
  • 16
  • 37
  • Please copy-paste (as text) the complete and full output of the compiler into your question. And then show us (with e.g. a comment) where in the [**Minimal**, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) the error is. – Some programmer dude Mar 07 '18 at 08:00
  • 2
    Note that there are other problems with your code as well. For example, uninitialized local variables (like `number` in your `main` function) really *are* uninitialized. Their values are *indeterminate* and will seem random. Don't use variables without initialization. – Some programmer dude Mar 07 '18 at 08:01
  • how do you solve your problem which you say produces other bugs? because obviously, you cannot perform `!=` between `int` and `struct` – Wreigh Mar 07 '18 at 08:06
  • 1
    As for your problem, is the purpose of the comparison `*itemtolist != 0` to check for a null pointer returned by `malloc`? Then you should probably reread your text books about pointers and the dereference operator `*` and what it does. – Some programmer dude Mar 07 '18 at 08:27
  • 1
    Oh and you might want to learn that there might be reasons [to not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Mar 07 '18 at 08:31
  • It would appear that you have invalid operands to the != operator. How much clearer can a compiler message get? Look at the operands then fix them. – Lundin Mar 07 '18 at 10:26

1 Answers1

0

You want to check that itemtolist itself (a pointer) isn't a null pointer.

But instead, your code attempts to compare *itemtolist (the pointed-at list), which isn't comparable to 0.

This fragment should be improved:

grocerylist * itemtolist;
itemtolist = (grocerylist*)malloc(sizeof(grocerylist)*arr);

if (*itemtolist != 0)

I'd write this as:

grocerylist *itemtolist = malloc((sizeof *itemtolist) * arr);

if (itemtolist)

Notes:

  • itemtolist != 0 is equivalent to just itemlist here in a boolean context. You can write it the long way if you want, but the short form is probably more idiomatic in C.
  • We don't cast the result of any of the malloc() family of functions.
  • Use the sizeof operator on the *itemlist so it automatically uses the correct type.
  • Initialize the variable as you declare it - that helps avoid accidentally using unintialized variables (but your compiler warnings should include that).

Also note that you really ought to check the return value from scanf() - I assume you removed the checking to make your example short for the question. You will need to change %c to %99s, but again, compiling with gcc -Wall or equivalent will help you spot that.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103