Doing the mod of a pointer variable in C
Вопрос
I am using embedded C for motorola 68000 microprocessor. I want to store the pointer address in a uint16 variable and then, want to do the mod of the variable. My main reason for doing this is to check the sectors of EEPROM are correctly assigned.
Eg->
uint16 a, c;
uint16 *b;
b=&a;
c=b%4;
Something like this. Obviously, I cannot do it this way.
Thanks
Решение
b = (uint16*)(((uint16)(&a)) % 4);
Другие советы
Are you limited to a 16-bit address space, i.e. 64 KB of RAM? That's quite small for a 68k system.
Anyway, you need to convert the pointer to an integer type in order to arithmetic on it;
#include <stdint.h>
uint16 a, *b = &a;
intptr_t c = (intptr_t) b;
if( (c % 4) != 0 )
{
/* bad address! */
}
Note that the above doesn't store the address in a 16-bit variable, since that's not the right way to do this. Instead, it uses intptr_t
to hold the pointer as an integer.
Не связан с StackOverflow