-1

我收到以下与某些函数的头文件相关的错误,我不确定如何解决它们:

prog8.c: In function 'main':
prog8.c:17: error: expected expression before ']' token
prog8.c:20: warning: ISO C90 forbids mixed declarations and code
prog8.c:21: error: expected expression before ']' token
prog8.c:25: warning: ISO C90 forbids mixed declarations and code
prog8.c:26: error: expected expression before ']' token
prog8.c:29: warning: ISO C90 forbids mixed declarations and code
prog8.c:30: error: expected expression before ']' token

下面是头文件代码:

#ifndef ARRAYFUNCTIONS_H_INCLUDED
#define ARRAYFUNCTIONS_H_INCLUDED

int arraySmallest(int [], int );

int arrayLargest(int [], int );

int arraySum(int [], int );

int arrayAverage(int [], int);

#endif

C 文件:

#include <stdlib.h>
#include <stdio.h>
#include "ArrayFunctions.h"

/* Smallest */

int arraySmallest(int array[], int size)
{
int i, min = array[0];
for (i = 0; i < size; i++)
{
    if (array[i] < min)
    {
        min = array[i];
    }
}
return min;
}

/* Largest */

int arrayLargest(int array[], int size)
{
int i, max = array[0];
for (i = 0; i < size; i++)
{
    if (array[i] > max)
    {
        max = array[i];
    }
}
return max;
}

/* Sum */

int arraySum(int array[], int size)
{
int i, sum = array[0];
for (i = 0; i < size; i++)
{
    sum += array[i];
}
return sum;
}

/* Sum */

int arrayAverage(int array[], int size)
{
int i, avg = array[0], sum = array[0];
for (i = 0; i < size; i++)
{
    sum += array[i];
}
avg = sum / size
return avg;
}

这是驱动程序:

#include <stdio.h>
#include "ArrayFunctions.h"

int main()
{

/* smallest */
int array[5] = {2, 8, 3, 4, 5};
printf("%d/n", (arraySmallest(array[], 5)));

/* largest */
int array2[5] = {2, 8, 3, 4, 5};
printf("%d/n", (arrayLargest(array2[], 5)));


/* sum */
int array3[5] = {2, 8, 3, 4, 5};
printf("%d/n", (arraySum(array3[], 5)));

/* average */
int array4[5] = {2, 8, 3, 4, 5};
printf("%d/n", (arrayAverage(array4[], 5)));

return 0;
}

非常感谢您提前提供的任何帮助!

4

2 回答 2

3

C 文件中的函数 defs 在 def 行的末尾有分号。

例如:

8: int arrayLargest(int array[], int size);
于 2013-11-04T02:42:44.353 回答
1

您的参数要求您传递一个数组,但您只传递一个整数:

int array[5] = {2, 8, 3, 4, 5}; 
printf("%d/n", (arraySmallest(array[5], 5)));
// ---------------------------------^

您对所有呼叫执行此操作。只需删除[5].

于 2013-11-04T03:00:42.997 回答