error: invalid static_cast from type ‘unsigned char*’ to type ‘uint32_t* {aka unsigned int*}’
uint32_t *starti = static_cast<uint32_t*>(&memory[164]);
I've allocated an array of chars, and I want to read 4 bytes as a 32bit int, but I get a compiler error. I know that I can bit shift, like this:
我已经分配了一个字符数组,我想把4个字节读为32位int,但是我得到了一个编译器错误。我知道我可以改变,像这样:
(start[0] << 24) + (start[1] << 16) + (start[2] << 8) + start[3];
And it will do the same thing, but this is a lot of extra work.
它会做同样的事情,但这是很多额外的工作。
Is it possible to just cast those four bytes as an int somehow?
是否可以将这四个字节转换成int类型?
4 个解决方案
#1
8
static_cast
is meant to be used for "well-behaved" casts, such as double -> int
. You must use reinterpret_cast
:
static_cast应该用于“行为良好的”类型转换,比如double -> int。
uint32_t *starti = reinterpret_cast<uint32_t*>(&memory[164]);
Or, if you are up to it, C-style casts:
或者,如果你能胜任,c风格的演员:
uint32_t *starti = (uint32_t*)&memory[164];
#2
6
Yes, you can convert an unsigned char*
pointer value to uint32_t*
(using either a C-style cast or a reinterpret_cast
) -- but that doesn't mean you can necessarily use the result.
是的,您可以将未签名的char*指针值转换为uint32_t*(使用c样式的转换或重新解释的cast)——但这并不意味着您一定可以使用结果。
The result of such a conversion might not point to an address that's properly aligned to hold a uint32_t
object. For example, an unsigned char*
might point to an odd address; if uint32_t
requires even alignment, you'll have undefined behavior when you try to dereference the result.
这样一个转换的结果可能不会指向一个正确对齐的地址以持有uint32_t对象。例如,一个未签名的char*可能指向一个奇数地址;如果uint32_t需要对齐,那么当您试图取消结果时,就会有未定义的行为。
If you can guarantee somehow that the unsigned char*
does point to a properly aligned address, you should be ok.
如果您可以保证没有签名的char*确实指向一个正确对齐的地址,那么应该可以。
#3
0
I am used to BDS2006 C++ but anyway this should work fine on other compilers too
我习惯了BDS2006 c++,但不管怎样,这也适用于其他编译器。
char memory[164];
int *p0,*p1,*p2;
p0=((int*)((void*)(memory))); // p0 starts from start
p1=((int*)((void*)(memory+64))); // p1 starts from 64th char
p2=((int*)((void*)(&memory[64]))); // p2 starts from 64th char
#4
0
You can use reinterpret_cast
as suggested by faranwath but please understand the risk of going that route.
您可以按照faranwath的建议使用reviewt_cast,但是请理解走这条路线的风险。
The value of what you get back will be radically different in a little endian system vs a big endian system. Your method will work in both cases.
在一个小的endian系统和一个大的endian系统中,你得到的东西的价值将会完全不同。你的方法在这两种情况下都适用。
#1
8
static_cast
is meant to be used for "well-behaved" casts, such as double -> int
. You must use reinterpret_cast
:
static_cast应该用于“行为良好的”类型转换,比如double -> int。
uint32_t *starti = reinterpret_cast<uint32_t*>(&memory[164]);
Or, if you are up to it, C-style casts:
或者,如果你能胜任,c风格的演员:
uint32_t *starti = (uint32_t*)&memory[164];
#2
6
Yes, you can convert an unsigned char*
pointer value to uint32_t*
(using either a C-style cast or a reinterpret_cast
) -- but that doesn't mean you can necessarily use the result.
是的,您可以将未签名的char*指针值转换为uint32_t*(使用c样式的转换或重新解释的cast)——但这并不意味着您一定可以使用结果。
The result of such a conversion might not point to an address that's properly aligned to hold a uint32_t
object. For example, an unsigned char*
might point to an odd address; if uint32_t
requires even alignment, you'll have undefined behavior when you try to dereference the result.
这样一个转换的结果可能不会指向一个正确对齐的地址以持有uint32_t对象。例如,一个未签名的char*可能指向一个奇数地址;如果uint32_t需要对齐,那么当您试图取消结果时,就会有未定义的行为。
If you can guarantee somehow that the unsigned char*
does point to a properly aligned address, you should be ok.
如果您可以保证没有签名的char*确实指向一个正确对齐的地址,那么应该可以。
#3
0
I am used to BDS2006 C++ but anyway this should work fine on other compilers too
我习惯了BDS2006 c++,但不管怎样,这也适用于其他编译器。
char memory[164];
int *p0,*p1,*p2;
p0=((int*)((void*)(memory))); // p0 starts from start
p1=((int*)((void*)(memory+64))); // p1 starts from 64th char
p2=((int*)((void*)(&memory[64]))); // p2 starts from 64th char
#4
0
You can use reinterpret_cast
as suggested by faranwath but please understand the risk of going that route.
您可以按照faranwath的建议使用reviewt_cast,但是请理解走这条路线的风险。
The value of what you get back will be radically different in a little endian system vs a big endian system. Your method will work in both cases.
在一个小的endian系统和一个大的endian系统中,你得到的东西的价值将会完全不同。你的方法在这两种情况下都适用。