[英]Convert char array to void * pointer in C
I want to read an address ( Like 0x827483 ) from input, and save this address in a void * variable. 我想从输入中读取一个地址(如0x827483),然后将此地址保存在void *变量中。 first i make this address as long long variable: 首先,我将此地址设为long long变量:
while(c != ' ' && !feof(file))
{
if('0' <= c && c <= '9')
num = c - '0';
else if('a' <= c && c <= 'f')
num = c - 'a' + 10;
b = b * 16 + num;
c = fgetc(file);
s[i++] = c;
}
and then i cast it to void *. 然后我将其转换为空*。
void * adr = (void *) b;
my code is working, but i got warning. 我的代码正在运行,但收到警告。
What can i do? 我能做什么?
The warning is telling you the long long
integer and the void *
are different sizes. 警告是告诉您long long
整数和void *
的大小不同。 Most likely, the long long
is 64 bit while the void *
is 32 bit. 最有可能的是, long long
是64位,而void *
是32位。
If you only need to build 32 bit code then you can use void * adr = (void *)(long)b
. 如果只需要构建32位代码,则可以使用void * adr = (void *)(long)b
。
I think a better solution is to use uintptr_t
instead of long long
. 我认为更好的解决方案是使用uintptr_t
而不是long long
。 That will insure the sizes are compatible for both 32 and 64 bit code. 这样可以确保大小兼容32位和64位代码。
As a side note, reading a memory address from a file is an insecure and dangerous practice. 附带说明,从文件中读取内存地址是一种不安全且危险的做法。 You need to validate that the pointer is something useful before you use it in any way. 在以任何方式使用指针之前,您需要验证指针是否有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.