cstructtypesfunc

a parameter list without types is only allowed in a function definition; in struct (C)


I wrote a struct in C and wanted to print it with a separated func but when I try to compile it I get said error form the title (a parameter list without types is only allowed in a function definition) in regards to the marked line in my code:

#include <stdio.h>
#include <string.h>

typedef struct 
{
    char Name[20];
    char Farbe[20];
    int Laenge;
    int Bewertungen[7];
} Gemüse;

void Gemüse_ausgeben(Gemüse gemüse);

int main(void)
{

Gemüse Gurke;   
strcpy(Gurke.Name, "Gurke");
strcpy(Gurke.Farbe, "grün");
Gurke.Laenge = 28;
Gurke.Bewertungen[0] = 8;
Gurke.Bewertungen[1] = 7;
Gurke.Bewertungen[2] = 9;
Gurke.Bewertungen[3] = 6;
Gurke.Bewertungen[4] = 4;
Gurke.Bewertungen[5] = 2;
Gurke.Bewertungen[6] = 5;

 void Gemüse_ausgeben(Gurke);     // line of error

return 0;  
}


void Gemüse_ausgeben(Gemüse gemüse)
{
printf("Name: %s\n", gemüse.Name);
printf("Farbe: %s\n", gemüse.Farbe);
printf("Laenge: %d\n", gemüse.Laenge);
printf("Bewertungen: \n"); 
for(int i = 0; i < 7; i++)
{
    printf("%d: %d\n", i+1, gemüse.Bewertungen[i]);
}

}

The variables might be slightly confusing.

I expected the code to compile without any error. When I change the error causing line to: void Gemüse_ausgeben(Gemüse Gurke); it compiles just fine but when carrying out the code nothing happens. In addition when I paste the body of my void Gemüse_ausgeben func into the main and get rid of the func everything works just fine. But that defeats the purpose for me.


Solution

  • This is a function declaration:

    void Gemüse_ausgeben(Gemüse gemüse);
    

    This is a function definition:

    void Gemüse_ausgeben(Gemüse gemüse) {
      ...
    }
    

    This is a function call:

    Gemüse_ausgeben(g);
    

    Note the syntax differences here. When declaring or defining you need to specify a return type. When calling you do not. For non-void returning functions you may want to capture the return value, like int x = y() but that's incidental.