C语言 “错误:在数字常量之前预期为 ';'、'、' 或 ')'”出现在我的代码中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35544519/
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:20:20  来源:igfitidea点击:

"error: expected ';', ',' or ')' before numeric constant" is coming up to my code

cfunctionaverage

提问by madLad

The program is about function making an average. I get an error:

该程序是关于函数求平均值。我收到一个错误:

error: expected ';', ',' or ') before numeric constant

错误:在数字常量之前应有 ';'、',' 或 ')

within the avg_array() function whenever I build it. Help would be appreciated, thanks!

每当我构建它时,在 avg_array() 函数中。帮助将不胜感激,谢谢!

#include <stdio.h>      
#define SIZE 5

// Prototypes
int avg_array (int*, int);          

main()
{
    int values[SIZE];
    int avg;
    int i;

    printf("Enter 5 numbers please. \n");

    for(i=0; i<SIZE; i++)
    {   
        scanf("%d", &values[i]);
    }

    avg = avg_array(values, SIZE);
    printf("\n The avg of the array is %d \n", avg);

    getchar();
    getchar();
} // end main()


/* Implement avg_array() WHERE THE ERROR PERTAINS */
avg_array(int my_array[], int SIZE)
{
    int sum;
    int i;
    int fxn_average;

    for(i=0; i<SIZE; i++)
    {
        sum = sum + my_array[i];
    }

    fxn_average = (sum/SIZE);

    return (fxn_average);
}

回答by e0k

You are using the identifier SIZEas an argument. This is also a macro that gets converted to 5by the preprocessor. After the preprocessor applies the macros, it would look like

您正在使用标识符SIZE作为参数。这也是一个被5预处理器转换成的宏。在预处理器应用宏之后,它看起来像

avg_array (int my_array[], int 5)

Since 5is a numeric constant instead of an identifier, it generates an error. Change the variable name.

由于5是数字常量而不是标识符,因此会产生错误。更改变量名称。

It looks like you also have a function signature missing a return type, which should match the prototype declared above. Try this instead:

看起来您还有一个缺少返回类型的函数签名,它应该与上面声明的原型相匹配。试试这个:

int avg_array (int *my_array, int size)
{
    int sum = 0;
    int i;

    for(i=0; i<size; i++)
    {
       sum = sum + my_array[i];
    }
    return sum/size;
}

The variable sumshould be initialized to 0. The local variable fxn_averageis not needed because you can use just return sum/size;at the end instead.

该变量sum应初始化为 0。fxn_average不需要局部变量,因为您可以只return sum/size;在最后使用。



I changed the type of the first argument from int[](array of int) to int *(pointer to int) so the function definitionmatches the prototype given in the question. The function was declaredas

我将第一个参数的类型从int[](array of int) 更改为int *(pointer to int),因此函数定义与问题中给出的原型相匹配。该函数被声明

int avg_array (int*, int);

These arguments have no identifiers; only their types are specified. This is valid C, but many style guides prescribe against it since naming arguments helps the reader understand meaning or intent. If you are writing a programming interface, for example, all the programmer will likely see is the function prototypes in a header file. It must be clear what the arguments are to write a correct function call. Anyway, adding in identifiers looks like:

这些参数没有标识符;仅指定了它们的类型。这是有效的 C,但许多风格指南都反对它,因为命名参数有助于读者理解含义或意图。例如,如果您正在编写编程接口,那么程序员可能看到的只是头文件中的函数原型。必须清楚参数是什么才能编写正确的函数调用。无论如何,添加标识符看起来像:

int avg_array (int *my_array, int size);

which is the same as in the definitionI used above.

这与我上面使用的定义相同。