取指定数值的地址 (int 转 void *)

int a = 0x12345678 是一个地址

void *p = (void *)a; 提示下马错误;

Error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]

This error occurs when there is an attempt to convert an integer to a pointer of a different size. This can happen when working with pointers and integers in C or C++.

To fix this error, you can try the following steps:

  1. Check the pointer type and size: Make sure that the pointer is the correct type and size for the data it is pointing to.

  1. Use the correct casting: Check that the correct casting is used when converting between pointers and integers. For example, use intptr_t or uintptr_t when converting between pointers and integers.

  1. Use the correct data type: If possible, use the correct data type for the data being worked with. For example, use long long instead of int if the data requires a larger integer.

It is important to note that casting between pointers and integers can be dangerous and can lead to undefined behavior. It is recommended to avoid casting when possible and to use the correct data types and casting methods when necessary.

正确的解决方案;

#include <stdint.h>

int a = 0x12345678 是一个地址

uintprt_t addr = a;

void *p = (void *)addr;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值