Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 返回存储器地址的函数_C - Fatal编程技术网

C 返回存储器地址的函数

C 返回存储器地址的函数,c,C,我试图编写一个程序,读取数组中的数值,调用一个函数来输入这些数值,然后计算平均值。从那里,它将调用另一个函数来打印相应的消息。问题是它似乎返回的是内存地址 提前谢谢 这是我的密码: #include <stdio.h> #include <math.h> int compute_grade_avg(int grades[], int size) { int i, result, average, sum=0; //Ask the user for the s

我试图编写一个程序,读取数组中的数值,调用一个函数来输入这些数值,然后计算平均值。从那里,它将调用另一个函数来打印相应的消息。问题是它似乎返回的是内存地址

提前谢谢

这是我的密码:

#include <stdio.h>
#include <math.h>

int compute_grade_avg(int grades[], int size)
{

  int i, result, average, sum=0;

  //Ask the user for the specific grades                                                                                           
  for(i=0;i<size;i++){
    printf("Enter a grade:\n");
    scanf("%d", &grades[i]);
  }

  //Calculate the sum of values in the array                                                                                       
  for(i=0;i<size;i++){
    sum += grades[i];
  }

  //Calculate the average based off of the sum and size of the array                                                               
  average = sum/size;
  result = ceil(average);

  return result;
}
//Function to print the corresponding message                                                                                      
int write_grade_message()

{
  int average;

  //Messages to print depending on grade                                                                                           
  if (average < 60){
    printf("Failed semester - registration suspended");
  }
  else if(average < 69){
    printf("On probation for next semester");
  }
  else if(average < 79){
    printf("");
  }
  else if(average < 89){
    printf("Dean's list for the semester");
  }
  else if(average < 100){
    printf("Highest honors for the semester");
  }
  return(0);
}
//Main Function                                                                                                                    
int main()
{
  int size, average;

  //Enter the number of grades that will go into the array                                                                        \

  printf("Enter the number of grades to be entered:\n");
  scanf("%d", &size);

  int grades[size];


  //Call compute grade average function                                                                                            
  average = compute_grade_avg(grades, size);

  //Call write grade message function                                                                                              
  write_grade_message(average);

  return(0);
}
#包括
#包括
整数计算等级平均值(整数等级[],整数大小)
{
int i,结果,平均值,和=0;
//向用户询问具体等级
对于(i=0;i
  • 首先,如果您执行带有警告的原始代码,它会说 警告:此函数[-Wuninitialized]“average”未初始化使用

这是因为在函数的任何时候,都不会向用户询问
平均值。
稍微看一下代码,我们看到变量
average
被计算为
average=compute\u grade\u avg(grades,size);
。因此,必须做的是将变量
average
传递给函数write\u grade\u message。这就像
int write\u grade\u message(int average);
第二个“”将是一个空字符串,但空字符串在C中不是这样定义的,但我想到在两个引号之间加一个空格“”。第三,注释
输入将进入数组的分数
应该注释
/**/而不是/
。 整个计划是这样的:

#include <stdio.h>
#include <math.h>

int compute_grade_avg(int grades[], int size)
{

    int i, result, average, sum=0;

    //Ask the user for the specific grades                                                                                           
    for(i=0;i<size;i++){
        printf("Enter a grade:\n");
        scanf("%d", &grades[i]);
    }

    //Calculate the sum of values in the array                                                                                       
    for(i=0;i<size;i++){
        sum += grades[i];
    }

    //Calculate the average based off of the sum and size of the array                                                               
    average = sum/size;
    result = ceil(average);

    return result;
}
//Function to print the corresponding message                                                                                      
int write_grade_message(int average)

{
    //int average=0;

    //Messages to print depending on grade                                                                                           
    if (average < 60){
        printf("Failed semester - registration suspended");
    }
    else if(average < 69){
        printf("On probation for next semester");
    }
    else if(average < 79){
        printf(" ");
    }
    else if(average < 89){
        printf("Dean's list for the semester");
    }
    else if(average < 100){
        printf("Highest honors for the semester");
    }
    return(0);
}
//Main Function                                                                                                                    
int main()
{
    int size, average;

    /*Enter the number of grades that will go into the array */                                                                       \

    printf("Enter the number of grades to be entered:\n");
    scanf("%d", &size);

    int grades[size];


    //Call compute grade average function                                                                                            
    average = compute_grade_avg(grades, size);

    //Call write grade message function                                                                                              
    write_grade_message(average);

    return(0);
}
#包括
#包括
整数计算等级平均值(整数等级[],整数大小)
{
int i,结果,平均值,和=0;
//向用户询问具体等级

对于(i=0;i您的问题是
int write_grade_message();
是一个可以 任意数量的参数,但在
write\u grade\u message
中看不到它们

您必须这样声明:

int write_grade_message(int average)
{
    // your code
    if (average < 60){
        printf("Failed semester - registration suspended");
    }
    else if(average < 69){
        printf("On probation for next semester");
    }
    else if(average < 79){
        printf("");
    }
    else if(average < 89){
        printf("Dean's list for the semester");
    }
    else if(average < 100){
        printf("Highest honors for the semester");
    }

    return(0);
}

问题特别在于,您向
int write_grade_message()
传递了一个参数,该参数采用了未指定数量的参数,这使得代码可以编译,但其行为现在是未定义的

还有另一个未定义行为发生的原因,因为您在
write_grade_message()
中重新声明了
average
,并且您试图读取该值,但之前没有初始化它1。您需要将
write_grade_message()
更改为读取

void write_grade_message(int average) ...
并删除
average
的内部声明,您的代码可能会工作得很好,还可以阅读问题的注释以获得更多有用的提示

如果您的代码编译得很干净,并且没有发出任何警告或消息,那么您需要启用编译器的警告和诊断功能,这不仅有助于您识破有时可能会犯的一些愚蠢错误,而且应该有助于您深入学习该语言,因为您可以从警告消息中推断出一些有用的知识关于语言



1在C语言中,不显式初始化变量(除了全局变量或带有
静态
存储类的变量)会使变量未初始化。除初始化外,任何尝试使用该变量的行为都会调用所谓的未定义行为调用
写入等级消息(平均);
正在传递参数平均值。 但是,该函数没有参数,而是未初始化的局部变量平均值的声明

函数应该像这样更新

int write_grade_message(int average)
{
// int average; ======> then removed this line

您正在调用
write\u grade\u message(average)
,但是您声明/定义了
write\u grade\u message()
(没有参数)。这将是一个问题。您没有收到编译器警告?您在
write\u grade\u message()中声明的
average
甚至没有初始化。因此它是一个随机ish值。这应该是另一个编译器警告(未设置的变量正在使用)。您需要先从C语言书开始学习基础知识。恐怕没有其他方法了。
result=ceil(average);
毫无意义。
ceil()
用于将
浮点值
四舍五入到下一个
int
,但是
平均值
已经是
int
@lower一个函数
void foo();
可以用许多参数调用:
foo(1,2,3,4,5,6,7,8,9)
是合法的。但是对于
无效条(void);
你不能做
bar(1,2,3,4,5)
,编译器将给您错误,而不是警告。@Pablo是的,这很好,我混淆了
无效
的情况。尽管如此,OP的代码中仍然存在问题。执行代码不会“说出”您没有为其编写代码的任何内容。如果您在编译带有警告的代码,请将其更改为。
int write_grade_message(int average)
{
// int average; ======> then removed this line