Recently I was learning about arrays passing to functions (by passing their base address to a pointer defined as parameter in function and then using pointer arithmetic for extracting the whole array subsequently)
For practice I was asked to calculate the average marks of a class of 70 students with their marks listed in an array named "marks" and was asked to define a variable with parameter as a pointer and calculate average from there.
The data given to me was that student 1 scored 40 , student 2 scored 41, student 3 scored 42....and so on.

以下是我的try :

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

float average(int *b)
{
    int sum = 0;
    for (int i = 1; i <= 70; i++)
    {
        sum = sum + *b;
        b = b + 1;
    }
    printf("the value of sum is %d\n", sum); // this value is changing every time I run the program
    return (((float)sum) / 70);
}

int main()
{
    int marks[70];
    marks[0] = 40;

    for (int i = 0; i < 68; i++)
    {
        marks[i + 1] = marks[i] + 1;
    }

    printf("the value of marks of 10th child is %d\n", marks[9]); // Just for checking if I am correct!(yes! the ans does come out to be 49!)
    printf("the value of average marks of the class is %f\n", average(&marks[0]));

    return 0;
}

令我惊讶的是,每次我运行它时,它的价值都在不断变化.有人能给我一个提示吗?我哪里错了?

推荐答案

您的问题与您的数组未初始化的事实(如我的 comments 中提到的)有关.

它的内存已经分配,但它的数据仍然是随机的混乱. 幸运的是,您覆盖了数组中除最后一个条目以外的所有条目的数据.在这一点上,最后一个入口基本上是一个随机值.

这就是输出不断变化的原因,您的ActialBUG要简单一些.

在计算和的for循环中,从i = 0迭代到i = 67.因此,使用+1偏移量可以将所有条目从1更改为68,这样最后一个条目(marks[69])就不会受到影响.

已修复代码:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float average(int *b) {
  int sum = 0;
  for (int i = 1; i <= 70; i++) {
    sum = sum + *b;
    b = b + 1;
  }
  printf("the value of sum is %d\n",
         sum); // this value is changing every time I run the program
  return (((float)sum) / 70);
}

int main() {
  int marks[70];
  marks[0] = 40;
  for (int i = 0; i < 68; i++) {
    marks[i + 1] = marks[i] + 1;
  }
  printf("the value of marks of 10th child is %d\n",
         marks[9]); // Just for checking if I am correct!(yes! the ans does come
                    // out to be 49!)
  printf("the value of average marks of the class is %f\n", average(&marks[0]));

  return 0;
}

PS: 在average函数中,您使用pointer arithmetic遍历输入数组,这被许多人认为是不好的做法.此外,您基本上没有使用您创建的for循环增量变量(int i).执行此操作的一种更简单、更安全的方法是:

float average(int *b) {
  int sum = 0;
  for (int i = 0; i < 69; i++) {
    sum += b[i];
  }
  printf("the value of sum is %d\n",
         sum); // this value is changing every time I run the program
  return (((float)sum) / 70);
}

C++相关问答推荐

如何将一个integer与一个数组进行比较?

为指针 struct 创建宏

使用额外的公共参数自定义printf

GLIBC:如何告诉可执行文件链接到特定版本的GLIBC

CC2538裸机项目编译但不起作用

在移动数组元素时获得意外输出

Boyer Moore算法的简单版本中的未定义行为

关于scanf()和空格的问题

可变宏不能编译

C将数组传递给函数以修改数组

理解bzip2的BZ2_解压缩函数中的状态重新分配

C代码可以在在线编译器上运行,但不能在Leetcode上运行

GCC错误,共享内存未定义引用?

我应该在递归中使用全局变量吗

递归打印二维数组(C编程)

如何使这个While循环在新行上结束

如何将大写/小写土耳其字母相互转换?

C中的回文数字

基于蝶数恰好有8个除数的事实的代码

在C中打印指针本身