c语言2446错误,错误C2446:'==':没有从'int *'到'int'的转换(error C2446: '==' : no conversion from 'int *' to 'int')...

错误C2446:'==':没有从'int *'到'int'的转换(error C2446: '==' : no conversion from 'int *' to 'int')

我想将一个数字与一个地址进行比较。 这是代码。

#include "stdio.h"

int main (void) {

int a = 1;

printf("%#x\n", &a); // The address of a is 0x18ff44

int b = 0x18ff44;

if (b == &a) { //error

printf("no\n");

}

return 0;

}

我想知道我是否可以将数字与地址进行比较。

错误:错误C2446:'==':没有从'int *'转换为'int'

我做了一些研究,我有两个假设:

也许是因为不同的数据类型无法比较。

但我试图将float的数据类型与char进行比较,并且代码被证明是正确的。

我学习了MSDN的编译器错误c2446。 翻译是“编译器无法将type1转换为type2”。

我的假设是,当执行关系操作时,编译器需要将type1转换为type2。 但是'int *'到'int'的转换没有意义。 我的问题:这个假设是否正确? 如果不是,为什么会发生这种情况?

I want to compare a number with a address. Here's the code.

#include "stdio.h"

int main (void) {

int a = 1;

printf("%#x\n", &a); // The address of a is 0x18ff44

int b = 0x18ff44;

if (b == &a) { //error

printf("no\n");

}

return 0;

}

I want to know whether I can compare a number with a address.

error: error C2446: '==' : no conversion from 'int *' to 'int'

I did some research and I have two hypothesises:

maybe because different datatype can't compare.

But I tried to compare datatype of float with char, and the code is compielred correct.

I learned the compiler error c2446 of MSDN. The translation is that "The compiler is unable to convert type1 to type2. "

My hypothesises is that when execute relational operation ,the compiler need to covert type1 to type2. But the conversion of 'int *' to 'int' does not make sense. My question: Is this hypothesis correct? And if it's not, why does that happens?

Compiler Info: VC 6.0 downloaded from https://www.microsoft.com

原文:https://stackoverflow.com/questions/43350786

更新时间:2019-10-24 10:10

最满意答案

b ( int )的类型与表达式&a ( int * )的类型不兼容 。 修复很简单 - 将b声明为int * :

int *b = (int *) 0x18ff44; // literal will have integral, non-pointer type,

... // so the cast is necessary

if ( b == &a ) // int * == int *

{

...

}

现在表达式具有兼容的类型,错误将消失。

要打印指针值,请使用%p转换说明符,如下所示:

printf( "&a = %p\n", (void *) &a );

这是C中极少数(如果不是唯一的)地方之一,需要投射void * 。

编辑

要回答更大的问题, 为什么 int与int *不兼容?

int数据类型是带符号的类型,仅保证表示[-32767,32767]范围内的值。 它可能能够表示更广泛的值(并且在大多数现代实现中,它通常[-2 32 , 2 32 -1] ),但它可能或可能不能表示所有可能的指针值。 指针值可以转换为int值,反之亦然,但结果不能保证有意义或定义明确。

指针类型的表示基本上取决于实现,并且该表示甚至可能不是标量值。 对于不同的指针类型,它甚至可能是不同的(尽管void *和char *必须具有相同的表示和对齐)。 因此, int和指针类型之间的直接比较可能没有意义。 实现可以在底层使用相同的字大小和表示,但由于不能保证这种情况,语言要求类型不兼容。

The type of b (int) is incompatible with the type of the expression &a (int *). The fix is simple - declare b as an int *:

int *b = (int *) 0x18ff44; // literal will have integral, non-pointer type,

... // so the cast is necessary

if ( b == &a ) // int * == int *

{

...

}

Now the expressions have compatible types, and the error will go away.

To print pointer values, use the %p conversion specifier as so:

printf( "&a = %p\n", (void *) &a );

This is one of the very few (if not only) places in C were casting a void * is necessary.

EDIT

To answer the larger question, why is int incompatible with int *?

The int datatype is a signed type, and is only guaranteed to represent values in the range [-32767,32767]. It may be able to represent a wider range of values (and on most modern implementations, it does, usually [-232, 232-1]), but it may or may not be able to represent all possible pointer values. A pointer value can be converted to an int value and vice versa, but the result is not guaranteed to be meaningful or well-defined.

Representation of pointer types is basically up to the implementation, and that representation may not even be a scalar value. It may even be different for different pointer types (although void * and char * must have the same representation and alignment). So a direct comparison between an int and a pointer type may not be meaningful. The implementation may use the same word size and representation for both under the hood, but since that's not guaranteed to be the case, the language mandates that the types are not compatible.

2017-04-11

相关问答

我们来看一下这一行: return y == null ? null : y.intValue();

在一个? : ? :语句,双方的:必须有相同的类型。 在这种情况下,Java将使其具有类型Integer 。 Integer可以为null ,所以左边是可以的。 表达式y.intValue()的类型为int ,但Java将自动将其自动y.intValue()为Integer (请注意,您也可以编写y ,这将为您保存此自动框)。 现在,结果必须再次取消装箱到int ,因为方法的返回类型是int 。

...

由于声明: int dp [len1] [len2]; dp[i-1,j]是一个数组 ,不是矩阵的一个元素 。 (这里comma operator可能会令人困惑,正如@BartoszKP所解释的那样。) 也许 minimu(dp[i-1,j]+1,dp[i,j-1]+1,dp[i-1][j-1]+k);

应该 minimu(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+k);

// ^ ^

PS请注意int dp[len1]

...

从这个std::tgamma参考 : 如果arg是自然数,则std::tgamma(arg)是arg-1的阶乘。 如果参数是一个足够小的整数,许多实现计算确切的整数域因子。 看来你正在使用的编译器正在这样做,计算表达式std::tgamma(7+1)的7阶乘因子。 编译器之间的结果可能有所不同,也可能在优化级别之间不同。 正如Jonas所示, 优化和未优化版本之间存在很大差异。 From this std::tgamma reference: If arg is a natural number,

...

像这样使用用户定义的转换 int type;

explicit operator int()

{

return type;

}

Something like this using user defined conversions int type;

explicit operator int()

{

return type;

}

我知道这将是一件愚蠢的事情,我需要使用SqlDbType而不是DbType。 现在工作正常 I knew it was going to be something silly, I need to use SqlDbType not DbType. Works fine now

b ( int )的类型与表达式&a ( int * )的类型不兼容 。 修复很简单 - 将b声明为int * : int *b = (int *) 0x18ff44; // literal will have integral, non-pointer type,

... // so the cast is necessary

if ( b == &a ) // int * == int *

{

...

}

现在表达式具有兼容的类型,错误将消

...

一致 while (*p != json)

你比较一个char类型的*p到json ,根据上面的代码应该是一个指针,我假设它是`const char *类型。 所以你应该这样做 while (p != json) ...

In line while (*p != json)

you compare a *p which is of type char to json, which, according to the code above should be a pointer, I a

...

写吧 int const *a; // or const int *a; which is the same.

......然后将保留const正确性。 编译器抱怨是因为你试图将v[i] (一个int const * )分配给int * ,通过它可以改变v promised不会改变的元素。 由于您以后不尝试这样做,只需使用int const*来确保编译器的安全。 请注意, a将保持指针变量(因此您可以重新分配它),只会指向整数常量(然后您无法通过a更改)。 要声明一个常量指针,你会写 int

...

int frequency = new int [fSize]; //invalid conversion error?

new返回指向堆分配对象的指针。 当你分配X , new返回一个X * 。 new X[n]分配new X[n] n实例并返回指向X的第一个实例的指针。 (在所有情况下, new也会调用已分配对象的构造函数,但这是一个无关紧要的点)。 new int [fSize];

因此,该表达式返回一个int * 。 int frequency

frequency显然是一个int 。

...

没有C ++ 11编译器的人写道 #define nullptr (0)

在头文件中的某个地方? (通常的宏是#define NULL (0) ) 当然,这是禁止的。 Someone without a C++11 compiler wrote #define nullptr (0)

somewhere in a header file? (The usual macro is #define NULL (0)) This is, of course, forbidden.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值