[error] invalid operands to binary - (have 'long long int' and 'int *')
时间: 2023-05-03 17:02:54 浏览: 301
这个错误是因为在一个减法运算中,两个操作数的类型不匹配。具体来说,左边的操作数是一个long long int类型,右边的操作数是一个int指针类型。减法运算符(-)只能用于相同类型的操作数。这意味着需要将右手边的操作数转换为long long int类型,以匹配左手边的操作数。
解决这个问题的方法是,使用类型转换来将指针类型转换为long long int类型。这可以通过在指针变量前面加上强制类型转换运算符(long long int)来实现。例如,如果右手边的操作数是一个指向int变量的指针,可以这样写:
long long int result = some_long_long_int - (long long int) some_int_pointer;
这样就可以将int指针类型的操作数转换为long long int类型,在减法运算中与long long int类型的操作数匹配,从而避免了类型不匹配的错误。
相关问题
error invalid operands to binary %(have double and int)
错误消息 "error: invalid operands to binary % (have double and int)" 是指在进行 C# 中的取模运算 (`%`) 时,操作数类型不匹配。`%` 运算符通常用于整数之间,但在这个情况下,你尝试对一个 `double` 类型的值与一个 `int` 类型的值进行操作。
在 C# 中,当你试图对不同类型的数值执行取模运算时,编译器无法确定如何处理这种混合操作。`double` 类型的数据不能直接与整数做 `%` 操作,因为它们的内部表示和规则不同于整数。
如果你确实需要得到两个数之间的余数,你应该确保至少有一个操作数是整数,或者如果两个都是 `double`,你需要先将其转换为整数进行计算。例如:
```csharp
// 如果double转int
int integerPart = (int)yourDoubleValue;
int remainder = integerPart % yourIntValue;
// 或者使用Math.Ceiling或Math.Floor保证除法结果为整数
double quotient = yourDoubleValue / yourIntValue;
int remainder = (int)Math.Floor(quotient * yourIntValue);
```
gcc编译出现:error: invalid operands to binary & (have ‘char *’ and ‘int *’)
这个错误通常是因为你在使用 & 运算符时,左边是一个指向字符数组的指针,右边是一个指向整数的指针,而这两种类型是不兼容的。
例如:
```c
char *str = "hello";
int *num_ptr = malloc(sizeof(int));
int result = str & num_ptr; // 这里会出现上述的错误
```
要解决这个问题,你需要确保两边的数据类型是兼容的,或者重新考虑你的代码逻辑。
如果你的意图是想将一个指针转换为整数类型,可以使用类型转换操作符将其强制转换为整数类型:
```c
char *str = "hello";
int num = (int)str; // 将指针转换为整数类型
```
但是需要注意的是,将指针转换为整数类型可能会导致一些未定义的行为,因为指针和整数类型的内存表示方式可能不同。因此,在进行此类转换时,需要确保你的代码逻辑是正确的。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)