简体   繁体   English

在 C99 的除法中使用指针,错误:二进制操作数无效

[英]Use of pointers in a division in C99, error: invalid operands to binary

The question surely look stupid, but I have always wasted a lot of time, with tests/errors until it works, with this kind of problem.这个问题肯定看起来很愚蠢,但我总是浪费很多时间,测试/错误,直到它工作,这种问题。

I need a pointer to return a value from a function, then I need to divide this value, but I have the compiler error:我需要一个指针来从 function 返回一个值,然后我需要将该值相除,但我有编译器错误:

invalid operands to binary / (have 'int *' and 'int')

Example in the following code:以下代码中的示例:

void test(int *x, int y)
{
    *x = *x+y;
}

int main()
{
    int *x = 20;
    int y = 1;

    test(&x, y);

    printf("x: %i", x);

    float res = x / 2; // error: invalid operands to binary / (have 'int *' and 'int')

    //float res = *x / 2; // application crash with this one. (GCC native Android)
}

I tried with '&' but it didn't work (x = 0), I try cast to int and only get the pointer adress, etc.我尝试使用 '&' 但它不起作用(x = 0),我尝试强制转换为 int 并且只获取指针地址等。

Your immediate problem is that you have declared x as a pointer to int, but you are trying to use it as an int.您的直接问题是您已将x声明为指向 int 的指针,但您正试图将其用作 int。 To simply correct the last line, dereference the pointer (just like you've correctly done inside test() ):要简单地更正最后一行,请取消引用指针(就像您在test()中正确完成的那样):

float res = *x / 2;

Now, it appears you actually tried that and got an error, which is not surprising, because you've initialized x badly:现在,您似乎确实尝试过并得到了一个错误,这并不奇怪,因为您初始化x很糟糕:

int *x = 20;

This doesn't create an int value of 20 and make x point to it.这不会创建 20 的 int 值并使 x 指向它。 It sets x to point at the memory address represented by the integer value 20. That's probably reserved memory, which is why you get an error when you try to dereference it.它将 x 设置为指向由 integer 值 20 表示的memory 地址。这可能是保留的 memory,这就是您尝试取消引用时出错的原因。

(You don't get that error in test() because you've passed the address of x as the argument. So dereferencing it there actually does get you 20 - probably.) (你不会在test()中得到那个错误,因为你已经将x的地址作为参数传递了。所以在那里取消引用它实际上确实得到了 20 - 可能。)

To make the pointers work, either do:要使指针起作用,请执行以下操作:

int x = 20;
...
test(&x, y)
...
float res = x / 2;

or:或者:

int *x = malloc(sizeof(int));
*x = 20;
...
test(x, y)
..
float res = *x /2;

But you are really making this too difficult.但你真的让这太难了。 Since you only need to output one value from the function, just make the function return that value.由于您只需要 output 来自 function 的一个值,因此只需让 function 返回该值。 Then you have no need to mess about with pointers at all:那么你根本不需要弄乱指针:

int test(x,y) { return x+y; }
...
int x = 20;
...
x = test(x,y);
...
float res = x / 2;

(And finally, I believe that in any case you want to use 2.0 in the last line, not just 2 , if you want to get a float result instead of an int.) (最后,我相信无论如何你都想在最后一行使用2.0 ,而不仅仅是2 ,如果你想得到一个浮点结果而不是一个 int 。)

int * x = 20; means: x is a pointer to an int stored at address 20 .表示: x是指向存储在地址20int的指针。 When you deference it, the CPU will try to read an int value from address 20 , yet address 20 is an invalid address in your current process space and thus your program crashes.当您服从它时,CPU 将尝试从地址20读取一个int值,但地址20在您当前的进程空间中是一个无效地址,因此您的程序会崩溃。

What you probably wanted do say is: x is a pointer to an int and the value of the int it points to is 20 , correct?您可能想要说的是: x是一个指向int的指针,它指向的int的值是20 ,对吗? Well, this would have been:好吧,这本来是:

int someInt = 20;
int * x = &someInt;

Now x points to whatever address someInt stores its value (actually it's on the stack space of the main function, but that's just a side node) and the value stored at that address is in fact 20 .现在x指向someInt存储其值的任何地址(实际上它在主 function 的堆栈空间上,但这只是一个侧节点)并且存储在该地址的值实际上是20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM