I have been working with pointers, and I am curious is it possible to add one (in hex) to a pointer so that if the location of the pointer is displayed I would get 0x445021 instead of 0x445020. I know that by saying *pa++ and the original address is 0x445020 i would get 0x445028. Or is it possible to force a variable to be at a certain location?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-11-05
Pointer arithmetic works in units determined by teh size of teh object to which it points.
Your point about *pa++ id not right, you dereferncing the pointer, so it would incremented the value pointerd to. If you meant pa++, the statement is only true if sizeof(pa) = 8 (a long long for example). If pa were a pointer to a char, the address increment would indeed be 1.
Consider teh following code. Note how the address after ++ is determined by the pointer type.
int main()
{
int array[] = { 0x11223344,0x55667788,3,4,5,6,7,8,9 };
int pa = array ;
char pb = (char*)array ;
I have been working with pointers, and I am curious is it possible to add one (in hex) to a pointer so that if the location of the pointer is displayed I would get 0x445021 instead of 0x445020. I know that by saying *pa++ and the original address is 0x445020 i would get 0x445028. Or is it possible to force a variable to be at a certain location?
Thanks
Pointer arithmetic works in units determined by teh size of teh object to which it points.
Your point about *pa++ id not right, you dereferncing the pointer, so it would incremented the value pointerd to. If you meant pa++, the statement is only true if sizeof(pa) = 8 (a long long for example). If pa were a pointer to a char, the address increment would indeed be 1.
Consider teh following code. Note how the address after ++ is determined by the pointer type.
int main()
{
int array[] = { 0x11223344,0x55667788,3,4,5,6,7,8,9 };
int pa = array ;
char pb = (char*)array ;
}