Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.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_Arrays_Function - Fatal编程技术网

C 下标值既不是数组也不是指针函数错误

C 下标值既不是数组也不是指针函数错误,c,arrays,function,C,Arrays,Function,开发一个程序,从几个文件中读取数据,扫描到数组中,最后在屏幕上打印13个名字,每个名字旁边有4个数字,这些数字后面有一个字母,就像是一种评分表 然而,我在使用一个函数时遇到了一个问题,这个函数的目的是计算平均值。它将一个学生测试的所有分数合并为一个值,然后将其除以4,得到平均值,然后将该平均值存储到不同数组的一个元素中 函数调用是: avg(&scores, &average); 分数和平均值的定义如下: int scores[13][4]; float average[13]

开发一个程序,从几个文件中读取数据,扫描到数组中,最后在屏幕上打印13个名字,每个名字旁边有4个数字,这些数字后面有一个字母,就像是一种评分表

然而,我在使用一个函数时遇到了一个问题,这个函数的目的是计算平均值。它将一个学生测试的所有分数合并为一个值,然后将其除以4,得到平均值,然后将该平均值存储到不同数组的一个元素中

函数调用是:

avg(&scores, &average);
分数和平均值的定义如下:

int scores[13][4];
float average[13];
void avg(int *scores, float *average)
{
int total1 = scores[0][0] + scores[0][1] + scores[0][2] + scores[0][3];
int total2 = scores[1][0] + scores[1][1] + scores[1][2] + scores[1][3];
int total3 = scores[2][0] + scores[2][1] + scores[2][2] + scores[2][3];
int total4 = scores[3][0] + scores[3][1] + scores[3][2] + scores[3][3];
int total5 = scores[4][0] + scores[4][1] + scores[4][2] + scores[4][3];
int total6 = scores[5][0] + scores[5][1] + scores[5][2] + scores[5][3];
int total7 = scores[6][0] + scores[6][1] + scores[6][2] + scores[6][3];
int total8 = scores[7][0] + scores[7][1] + scores[7][2] + scores[7][3];
int total9 = scores[8][0] + scores[8][1] + scores[8][2] + scores[8][3];
int total10 = scores[9][0] + scores[9][1] + scores[9][2] + scores[9][3];
int total11 = scores[10][0] + scores[10][1] + scores[10][2] + scores[10][3];
int total12 = scores[11][0] + scores[11][1] + scores[11][2] + scores[11][3];
int total13=  scores[12][0] + scores[12][1] + scores[12][2] + scores[12][3];

float avg1 = total1 / 4;
float avg2 = total2 / 4;
float avg3 = total3 / 4;
float avg4 = total4 / 4;
float avg5 = total5 / 4;
float avg6 = total6 / 4;
float avg7 = total7 / 4;
float avg8 = total8 / 4;
float avg9 = total9 / 4;
float avg10 = total10 / 4;
float avg11 = total11 / 4;
float avg12 = total12 / 4;
float avg13 = total13 / 4;

return;
}
并使用此循环填充分数:

for(i=0; i<=13; i++)
{
    for(j=0; j<=4; j++)
    {
    fscanf(score, "%d", &scores[i][j]);
    }
}
fclose(score);
函数本身如下所示:

int scores[13][4];
float average[13];
void avg(int *scores, float *average)
{
int total1 = scores[0][0] + scores[0][1] + scores[0][2] + scores[0][3];
int total2 = scores[1][0] + scores[1][1] + scores[1][2] + scores[1][3];
int total3 = scores[2][0] + scores[2][1] + scores[2][2] + scores[2][3];
int total4 = scores[3][0] + scores[3][1] + scores[3][2] + scores[3][3];
int total5 = scores[4][0] + scores[4][1] + scores[4][2] + scores[4][3];
int total6 = scores[5][0] + scores[5][1] + scores[5][2] + scores[5][3];
int total7 = scores[6][0] + scores[6][1] + scores[6][2] + scores[6][3];
int total8 = scores[7][0] + scores[7][1] + scores[7][2] + scores[7][3];
int total9 = scores[8][0] + scores[8][1] + scores[8][2] + scores[8][3];
int total10 = scores[9][0] + scores[9][1] + scores[9][2] + scores[9][3];
int total11 = scores[10][0] + scores[10][1] + scores[10][2] + scores[10][3];
int total12 = scores[11][0] + scores[11][1] + scores[11][2] + scores[11][3];
int total13=  scores[12][0] + scores[12][1] + scores[12][2] + scores[12][3];

float avg1 = total1 / 4;
float avg2 = total2 / 4;
float avg3 = total3 / 4;
float avg4 = total4 / 4;
float avg5 = total5 / 4;
float avg6 = total6 / 4;
float avg7 = total7 / 4;
float avg8 = total8 / 4;
float avg9 = total9 / 4;
float avg10 = total10 / 4;
float avg11 = total11 / 4;
float avg12 = total12 / 4;
float avg13 = total13 / 4;

return;
}
它还没有完成,我仍然需要告诉函数将avg1-avg13分配给数组。但一旦我修正了这个错误,我会继续努力的

试图按原样运行程序会给我带来很多错误,这些错误基本相同:

ghp11.c: In function 'avg':
ghp11.c:127: error: subscripted value is neither array nor pointer

我不确定如何修复它,使其正常工作。我试图将4个数组值组合成一个整数值,并将其存储在total1中,以此类推;这样就可以求出平均值并进行存储。

要将2D数组传递给函数,必须这样编写函数定义

void avg( int score[][4], float *average );

要将2D数组传递给函数,必须这样编写函数定义

void avg( int score[][4], float *average );

我注意到的第一件事是缓冲区溢出。for i和for j循环的溢出量为1。

我注意到的第一件事是缓冲区溢出。对于i和for j循环,溢出1。

在调用
avg
时,从
&scores
&average
中删除
&code>:

avg( scores, average );
avg
的原型更改为

void avg( int (*scores)[4], float *average ) // or int scores[][4]
{
  int i = 0;

  for ( i = 0; i < 13; i++ )
  {
    // you could write another loop for summing your total, 
    // but for just 4 elements writing it out isn't a big deal.

    int total = scores[i][0] + scores[i][1] + scores[i][2] + scores[i][3];
    average[i] = total / 4.0;
  }
}
并将
avg
的主体更改为

void avg( int (*scores)[4], float *average ) // or int scores[][4]
{
  int i = 0;

  for ( i = 0; i < 13; i++ )
  {
    // you could write another loop for summing your total, 
    // but for just 4 elements writing it out isn't a big deal.

    int total = scores[i][0] + scores[i][1] + scores[i][2] + scores[i][3];
    average[i] = total / 4.0;
  }
}
但是,由于我们以
int(*scores)[4]
的形式通过了它,所以我们可以编写

int total = scores[0][0] + scores[0][1] + ...;
因为下标操作
得分[0]
隐式取消引用指针

请注意,在您的代码中,您的平均值最终都将被截断为较低的整数值;整数除以整数得到整数结果,因此像
5/4
这样的表达式将产生1,
3/4
将产生0。如果要获取分数值,需要将其中一个操作数设为浮点:

average[i] = total / 4.0;
最后,您没有将结果平均值写入
average
数组;您只是创建并分配了一组变量,这些变量是
avg
函数的局部变量;一旦函数退出,那些
avgN
变量就会消失。在上面的代码中,我只是将
avgN
变量替换为
average
数组的元素

每当你发现自己在创建一堆变量,比如
avg1
avg2
avg3
,退一步,意识到你真正想要的是一个数组。同样,当你发现自己在写这样的陈述时

avg1 = total1 / 4.0;
avg2 = total2 / 4.0;
avg3 = total3 / 4.0;
这里真正需要的是一个循环:

for ( i = 0; i < N; i++ )
   avg[i] = total[i] / 4.0;
并称之为

size_t rows = sizeof scores / sizeof scores[0]; // divides the total number of bytes
                                                // in the array by the number of bytes
                                                // in a single element, giving the
                                                // number of elements in the array
avg( scores, average, rows );

当然,
scores
中的列数仍然固定为4。如果您想支持任意列数的分数,那么您必须做一些不同的事情。然而,我们将把它留到将来

在调用
avg
时,从
得分和
平均值中删除

avg( scores, average );
avg
的原型更改为

void avg( int (*scores)[4], float *average ) // or int scores[][4]
{
  int i = 0;

  for ( i = 0; i < 13; i++ )
  {
    // you could write another loop for summing your total, 
    // but for just 4 elements writing it out isn't a big deal.

    int total = scores[i][0] + scores[i][1] + scores[i][2] + scores[i][3];
    average[i] = total / 4.0;
  }
}
并将
avg
的主体更改为

void avg( int (*scores)[4], float *average ) // or int scores[][4]
{
  int i = 0;

  for ( i = 0; i < 13; i++ )
  {
    // you could write another loop for summing your total, 
    // but for just 4 elements writing it out isn't a big deal.

    int total = scores[i][0] + scores[i][1] + scores[i][2] + scores[i][3];
    average[i] = total / 4.0;
  }
}
但是,由于我们以
int(*scores)[4]
的形式通过了它,所以我们可以编写

int total = scores[0][0] + scores[0][1] + ...;
因为下标操作
得分[0]
隐式取消引用指针

请注意,在您的代码中,您的平均值最终都将被截断为较低的整数值;整数除以整数得到整数结果,因此像
5/4
这样的表达式将产生1,
3/4
将产生0。如果要获取分数值,需要将其中一个操作数设为浮点:

average[i] = total / 4.0;
最后,您没有将结果平均值写入
average
数组;您只是创建并分配了一组变量,这些变量是
avg
函数的局部变量;一旦函数退出,那些
avgN
变量就会消失。在上面的代码中,我只是将
avgN
变量替换为
average
数组的元素

每当你发现自己在创建一堆变量,比如
avg1
avg2
avg3
,退一步,意识到你真正想要的是一个数组。同样,当你发现自己在写这样的陈述时

avg1 = total1 / 4.0;
avg2 = total2 / 4.0;
avg3 = total3 / 4.0;
这里真正需要的是一个循环:

for ( i = 0; i < N; i++ )
   avg[i] = total[i] / 4.0;
并称之为

size_t rows = sizeof scores / sizeof scores[0]; // divides the total number of bytes
                                                // in the array by the number of bytes
                                                // in a single element, giving the
                                                // number of elements in the array
avg( scores, average, rows );
当然,
scores
中的列数仍然固定为4。如果您想支持任意列数的分数,那么您必须做一些不同的事情。然而,我们将把它留到将来