Reputation: 6638
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;
}
Upvotes: 0
Views: 205
Reputation: 91017
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.
Upvotes: 1