Search code examples
cstructtypename

Unknown type name when using struct in c


My goal is using struct to create a menu manager program and I took the idea from a student managing program. Here are some lines of my code:

#include<stdio.h>

typedef struct food{
    char foodname[111];
    char description[703];
    float price;
};

void add(food &food){
    float p;
    printf("Name: "); scanf("%110[^\n]", food.foodname);
    fflush(stdin);
    printf("Description: "); scanf("%702[\^n]", food.description);
    fflush(stdin);
    do{
        printf("Price: "); scanf("%lf", &food.price);
        p = food.price;
        if(p<=0){
            printf("Price should not be smaller or equal zero !\n");
        }
    } while (p<=0);
    printf("Added !");
}

int main(){
    food food1;
    add(food food1);
}

The original code's owner has a video about it and the code ran smoothly, so I actually just change sth like the variable name, struct name and the content in the printf. However, it showed 2 error. 1 is "expected ')' before '&' token" at

void add(food &food){...}

and 2 is "unknown type name 'food', use 'struct' keyword to refer to the type at

food food1;

The second error is easily fixed by adding 'struct' but I have learned that by using typedef, we can remove that syntax. What I have done wrong here ? By the way, I have run the original code and it shows the same errors. I'm using gcc 9.2.0 64bit while the owner ran the code on gcc 4.9.2 64bit, is that the main reason ?


Solution

  • You have two problems.

    The first is that typedef struct food{ ... }; is invalid.

    The syntax for typedef is

    typedef actual_type type_alias;
    

    And in your case the actual_type is the whole struct food { ... } part, so you need to do:

    typedef struct food {
        // ...
    } food;
    

    This should have been clearly shown in any decent book, tutorial or class.


    The second problem is that you attempt to pass the argument to add by reference, which is a C++-specific feature that doesn't exist in C.

    In C you can only emulate pass by reference, using pointers and the pointer-to operator to get a pointer to the variables you pass.

    You need to do something like:

    void add(food *food_ptr)
    {
        // Dereference the pointer food_ptr to get the object it points to
        // For example:
        float p = food_ptr->price;
    
        // ...
    }
    

    Then call it as:

    food f;
    add(&f);  // Pass a pointer to the structure object f
    

    While this might be more obscure than the other error, a good resource should have explanations about this as well.


    Lastly, you actually have another error...

    The C specification explicitly mention passing an input-only stream (like stdin) to the fflush function as leading to undefined behavior.

    Unfortunately one single compiler add this as a non-standard and non-portable extension to the C language. Using it is a bad habit that you should learn to not use.

    You need to figure out some other way to flush the input until newline.

    Also, using scanf to read lines is really not something any experienced C programmer recommend. Use fgets instead, and remove the possible trailing newline.