在结构体(C)中,只允许在函数定义中使用无类型的参数列表。

huangapple go评论75阅读模式
英文:

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;

    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 function into the main and get rid of the function, everything works just fine. But that defeats the purpose for me.

英文:

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 &lt;stdio.h&gt;
#include &lt;string.h&gt;

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

void Gem&#252;se_ausgeben(Gem&#252;se gem&#252;se);

int main(void)
{

Gem&#252;se Gurke;   
strcpy(Gurke.Name, &quot;Gurke&quot;);
strcpy(Gurke.Farbe, &quot;gr&#252;n&quot;);
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&#252;se_ausgeben(Gurke);     // line of error

return 0;  
}


void Gem&#252;se_ausgeben(Gem&#252;se gem&#252;se)
{
printf(&quot;Name: %s\n&quot;, gem&#252;se.Name);
printf(&quot;Farbe: %s\n&quot;, gem&#252;se.Farbe);
printf(&quot;Laenge: %d\n&quot;, gem&#252;se.Laenge);
printf(&quot;Bewertungen: \n&quot;); 
for(int i = 0; i &lt; 7; i++)
{
    printf(&quot;%d: %d\n&quot;, i+1, gem&#252;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.

答案1

得分: 1

这是一个函数声明:

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

这是一个函数定义:

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

这是一个函数调用:

Gemüse_ausgeben(g);

请注意这里的语法差异。在声明或定义函数时,您需要指定返回类型。在调用函数时则不需要。对于非void返回值的函数,您可能希望捕获返回值,例如 int x = y(),但这是次要的。

英文:

This is a function declaration:

void Gem&#252;se_ausgeben(Gem&#252;se gem&#252;se);

This is a function definition:

void Gem&#252;se_ausgeben(Gem&#252;se gem&#252;se) {
  ...
}

This is a function call:

Gem&#252;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.

答案2

得分: 0

早期 C 语言设计中令人困惑的一个方面是,函数的所有参数的名称都会按照参数传递的顺序指定,然后是那些不是 int 类型的参数的类型声明,顺序是任意的。因此,今天会这样写的定义:

int doSomething(int a, double b, int c, double d)
{ ... }

在早期可能会这样写:

int doSomething(a,b,c,d)
  double b,d;
{ ... }

或者:

int doSomething(a,b,c,d)
  int a; double b; int c; double d;
{ ... }

或者:

int doSomething(a,b,c,d)
  int a,c; double b,d;
{ ... }

还有其他各种各样的变化和排列方式。在所有情况下,用括号括起来的逗号分隔的参数列表只包括参数的名称,不包括有关它们类型的信息。

由于不是定义的函数声明没有地方列出参数类型,并且没有类型信息的参数名称列表通常不太有用,所以旧式声明(不是定义)根本不包括参数。当然,新式定义可以包括参数,但新式定义中的每个参数都包括类型以及名称。

英文:

A somewhat puzzling aspect of the design of early C is that the names of all of a function's arguments would be specified, in argument-passing order, followed by typed declarations of the any arguments that weren't int, specified in arbitrary order. Thus, a definition that today would be written as:

int doSomething(int a, double b, int c, double d)
{ ... }

could have been written as:

int doSomething(a,b,c,d)
  double b,d;
{ ... }

or as:

int doSomething(a,b,c,d)
  int a; double b; int c; double d;
{ ... }

or as:

int doSomething(a,b,c,d)
  int a,c; double b,d;
{ ... }

or other variations and permutations of those. In all cases, the parenthesis-enclosed comma-separated list of arguments includes just the names of the arguments, and no information about their types.

Since function declarations that aren't definitions would have no place to list argument types, and since a list of argument names without types wouldn't generally be very useful, old-style declarations that aren't definitions can't include arguments at all. New-style definitions can include arguments, of course, but each argument in a new-style definition includes the type as well as the name.

huangapple
  • 本文由 发表于 2023年6月9日 01:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定