I was wondering if theres a better way instead of casting a void pointer to uint8_t
, then do arithmetic based on an offset, then cast it to uint32_t
, then dereference it to get a 4byte value at that exact offset.
void * foo = malloc(1024);
uint32_t myVar;
for(int i = 0; i < 10; i++)
{
myVar = *((uint32_t*)((uint8_t *) foo + nOffset));
nOffset += 100;
}
You would better simply stay within one "domain" of pointer arithmetic.
You should do uint32_t * foo = malloc(1024);
and then either
myVar = foo + nOffset / sizeof(*foo);
nOffset += 100;
or
myVar = foo + nOffset;
nOffset += 100 / sizeof(*foo);
for advancing by 100 bytes at each step.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.