C语言 二进制表达式的无效操作数,C

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

Invalid Operands to binary Expression, C

c

提问by aprohl5

Im working on a function that takes some values and finds the min, max, and average of the values. I'm passing everything to the function by reference and am getting some errors when I try and do basic operations like +and /Namely the error is

我正在研究一个函数,它接受一些值并找到这些值的最小值、最大值和平均值。我通过引用将所有内容传递给函数,并且在尝试执行基本操作时遇到一些错误,例如+/即错误是

Invalid operands to binary expression ('double *' and 'double *')

二进制表达式的无效操作数('double *' 和 'double *')

void MinMaxAvg(double *pA, double *min, double *max, double *avg, int lines, double *total )
{
    for (int i=0; i<lines; i++)
    {
        if ( i==0)
        {
            min = &pA[0];
            max = &pA[0];
        }
        else
        {
            if (&pA[i] < min)
            {
                min = &pA[i];
            }

            if (&pA[i] > max)
            {
                max = &pA[i];
            }
        }

        total += &pA[i];     //<-- Errors on this line

    }

    avg = (total / lines);         // <-- Errors on this line.

}

回答by Magnus

It seems like you're getting some of the types confused there. In your example you're setting the pointersto a new value, not the value of said pointers.

似乎您在那里混淆了某些类型。在您的示例中,您将指针设置为新值,而不是所述指针的值。

The first would have to be:

第一个必须是:

*total += pA[i];

While the second should be:

而第二个应该是:

*avg = (*total / lines);

In fact, you probably want to use floating point division on the second error there (some compilers are notorious for using integer divison in unexpected places):

事实上,您可能想在第二个错误上使用浮点除法(一些编译器因在意想不到的地方使用整数除法而臭名昭著):

*avg = (*total / (double)lines);

You'll still be getting errors if you do it like that, however. For example &pA[i] > ...will result in a pointer comparison, i.e. the addressof the pointers will be compared. Most likely not what you want.

但是,如果您这样做,您仍然会遇到错误。例如&pA[i] > ...将导致指针比较,即将比较指针的地址。很可能不是你想要的。

回答by unwind

You're trying to add an address to a pointer, that's not a valid operation.

您正在尝试向指针添加地址,这不是有效的操作。

You probably meant:

你可能的意思是:

*total += pA[i];

Your use of &pAseems very confused, as does the re-assignment of the pointers minand max.

您的使用&pA似乎很混乱,指针min和的重新分配也是如此max

If you have a pointer to a value (like double *min), then *minis how you access (read or write) the value being pointed at.

如果您有一个指向值的指针(如double *min),那么*min就是您访问(读取或写入)所指向的值的方式。

回答by oleg_g

There is no "pass by reference" in C language unlike C++. You should dereference each pointer in your code if you want work with variables values. Now you work with variables addresses.

与 C++ 不同,C 语言中没有“通过引用传递”。如果您想使用变量值,您应该取消引用代码中的每个指针。现在您使用变量地址。