.cpp:13:21: error: cast from ‘int*’ to ‘int’ loses precision [-fpermissive] 13 | int size = (int)&_binary_TEST_DATJ_size;
时间: 2024-03-20 18:40:21 浏览: 49
这个错误表示您正在尝试将一个指针转换为整数,这可能会导致精度丢失。在这里,您正在将指向变量 `_binary_TEST_DATJ_size` 的指针转换为整数。如果您只是想获取变量的大小,那么您可以使用 `sizeof` 运算符来获取变量的大小,而无需将指针转换为整数。例如,您可以尝试使用以下代码:
```
int size = sizeof(_binary_TEST_DATJ_size);
```
这将返回 `_binary_TEST_DATJ_size` 变量所占用的字节数。
相关问题
[Error] cast from 'int*' to 'int' loses precision [-fpermissive]怎么解决
这个错误通常是因为将一个指针类型的变量直接赋值给整型变量导致的。解决方法是需要将指针类型的变量转换为整型变量。可以使用 C++ 中的类型转换函数 static_cast,如下所示:
```cpp
int* ptr = new int(10);
int num = static_cast<int>(*ptr); // 将指针类型的变量转换为整型变量
```
或者,如果你知道指针指向的具体值,也可以直接将其赋值给整型变量,如下所示:
```cpp
int* ptr = new int(10);
int num = *ptr; // 直接将指针指向的值赋值给整型变量
```
需要注意的是,如果指针指向的值超出了整型变量的表示范围,那么转换后的整型变量可能会出现截断或溢出等问题。
cast from 'int*' to 'int' loses precision [-fpermissive]
这个错误信息 "cast from 'int*' to 'int' loses precision [-fpermissive]" 是在C++编程中常见的一个编译错误。这个错误表明你试图将一个指向整数的指针(int*)强制转换为整数(int),这会导致精度丢失。让我们详细解释一下:
1. 指针类型 vs 基本数据类型:
- int* 是一个指向整数的指针。它存储的是一个内存地址。
- int 是一个基本数据类型,用于存储整数值。
2. 精度丢失:
- 指针通常占用 4 字节(在 32 位系统上)或 8 字节(在 64 位系统上)。
- int 通常占用 4 字节。
- 将一个 4 字节或 8 字节的指针转换为 4 字节的 int 会导致数据截断,从而造成精度丢失。
3. 错误原因:
这种转换通常是不正确的,因为指针和整数代表的是不同类型的数据。指针包含的是内存地址,而整数包含的是数值数据。
解决方法:
1. 检查代码逻辑,确保不需要这种转换。
2. 如果确实需要将指针转换为整数(不推荐),可以使用 uintptr_t 或 intptr_t 类型,它们专门用于存储指针地址。
3. 如果你的目的是获取数组中某个元素的地址,确保使用正确的语法。
例如,假设你有一个整数数组:
```cpp
int arr[10];
int* p = &arr[0];
```
如果你想获取第二个元素的地址,可以直接这样做:
```cpp
int* second = &arr[1]; // 正确,不需要转换
```
如果你想通过指针算术获取地址,可以这样做:
```cpp
int* second = p + 1; // 正确,指针算术
```
阅读全文
相关推荐

















