C语言 在 C 中未声明(首次在此函数中使用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35690805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:21:02  来源:igfitidea点击:

undeclared (first use in this function) in C

c

提问by DancingDylan

I know when you type this into search, there are a few similar "like" questions. Yet, all the ones I saw that weren't duplicate were from other sources besides C. Anyway, hope someone can help. I have a structure, declared in my header obviously.

我知道当你在搜索中输入这个时,会有一些类似的“喜欢”问题。然而,我看到的所有不是重复的都是来自除 C 之外的其他来源。无论如何,希望有人可以提供帮助。我有一个结构,显然在我的标题中声明。

struct Statistics //Super basic structure.
{
 int mean;
 int median;
 int variance;
 int grades;
 int min;
 int max;
}; 

All of these functions are working properly without the structure, that's why I'm trying to use one. For cleanliness, and just pure understanding. My error is this, I get the line 'stats' undeclared (first use in this function)when I call the function this structure is within.

所有这些功能在没有结构的情况下都能正常工作,这就是我尝试使用一个的原因。为了清洁,只是纯粹的理解。我的错误是这样的,当我调用这个结构所在的函数时,我得到了未声明的 'stats'(第一次在这个函数中使用)

This is where the error send me to:

这是错误将我发送到的地方:

display_grades_distribution(m, grades_scale,n, stats);

So my question is this, how could my compiler tell me that this structure is 'undeclared' and or that its the 'first use' in the program.

所以我的问题是,我的编译器怎么能告诉我这个结构是“未声明的”,或者它是程序中的“第一次使用”。

SNIPPET:

片段:

void display_grades_distribution(int m, int grades_scale[11][m], int n,         
struct Statistics stats[])
{
 printf("Mean = %22f\n", stats[0].mean);
 printf("Variance = %18f\n", stats[0].variance);
 printf("Median = %20f\n", stats[0].median);
 printf("min = %16d\n", stats[0].min);
 printf("max = %16d\n", stats[0].max);
}

int main(void)
{
 //-------------------------------------------
  for(m = 0, m < num_assignments; m++)
  {
  struct Statistics stats[5];
  stats[m].mean = calculate_mean(grades,n);
  stats[m].median = calculate_median(grades,n);
  stats[m].variance = calculate_mean(grades,n);
  stats[m].min = calculate_min(grades,n);
  stats[m].max = calculate_max(grades,n);
  }
 //-------------------------------------------
 **display_grades_distribution(m, grades_scale,n, stats);** //This causes the error.
}

回答by MikeCAT

struct Statistics stats[5];is local variable in the block used in forstatement, so it isn't visible after the forloop.

struct Statistics stats[5];for语句中使用的块中的局部变量,因此在for循环后不可见。

Get it out of forstatement.

把它从for声明中取出来。

int main(void)
{
  struct Statistics stats[5];
  //-------------------------------------------
  for(m = 0, m < num_assignments; m++)
  {
    stats[m].mean = calculate_mean(grades,n);
    stats[m].median = calculate_median(grades,n);
    stats[m].variance = calculate_mean(grades,n);
    stats[m].min = calculate_min(grades,n);
    stats[m].max = calculate_max(grades,n);
  }
  //-------------------------------------------
  display_grades_distribution(m, grades_scale,n, stats);
}