C语言 分配给类型时不兼容的类型 - C

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

Incompatible types when assigning to type - C

cpointerstypesdouble

提问by threeleggedrabbit

I'm working in Code::Blocks on a project in C.

我正在 Code::Blocks 中的一个 C 项目工作。

When I compile I get the error: "incompatible types when assigning to type 'double *' from type 'double'" on lines 81, 85, 90, 91.

当我编译时,在第 81、85、90、91 行出现错误:“从类型 'double' 分配给类型 'double *' 时类型不兼容”。

The project is to take a unit conversion tool and incorporate multiple functions instead of everything under the main().

该项目是采用单位转换工具并合并多个功能而不是main()下的所有内容。

http://pastebay.net/1181184

http://pastebay.net/1181184

回答by cnicutar

Try dereferencing the pointer:

尝试取消引用指针:

*pKelvin = PROD((fahrenheit+459.67),ytemp);
^

回答by templatetypedef

All of the errors you're getting are variations on a theme. Take this line, for example:

您遇到的所有错误都是主题的变体。以这一行为例:

pKelvin = PROD((fahrenheit+459.67),ytemp);

Here, pKelvinhas type double*, meaning that it's a pointer to an object of type double. On the other hand, the right-hand side has type double, meaning that it's an actual double. C is complaining because you can't assign doubles to double*s, since they represent fundamentally different types.

这里,pKelvin有 type double*,这意味着它是一个指向 type 对象的指针double。另一方面,右侧有 type double,这意味着它是一个实际的double. C 抱怨是因为您不能将doubles分配给double*s,因为它们代表根本不同的类型。

To fix this, you probably want to write

为了解决这个问题,你可能想写

*pKelvin = PROD((fahrenheit+459.67),ytemp);

This says "store the value of PROD((fahrenheit+459.67),ytemp)at the doublepointed at by pKelvin. This works because you're now assigning a doubleto an object of type double.

这表示“将 的值存储PROD((fahrenheit+459.67),ytemp)double指向的 处pKelvin。这是有效的,因为您现在正在将 a 分配给double类型为 的对象double

More generally, if you ever see an error like this one, it probably means you're assigning a pointer to a non-pointer or vice-versa.

更一般地说,如果您看到这样的错误,则可能意味着您正在将指针分配给非指针,反之亦然。

Hope this helps!

希望这可以帮助!