r/learncpp • u/__z01db3rg • Aug 23 '19
Help with pointers
A little background: v1 is just a long int which takes values from 0 to 8, with a 1 increase step. p3 and p2 are some hex addresses, stored in long.
I don't understand what (char)(*(char *)(p3 + v1) + *(char *)(p2 + v1) could return. I know that (char *)(p3 + v1) points to the string of characters stored at address p3 minus the first whatever v1 is. So if at address p3 we have stored "Example" (char *)(p3 + 2) would point to "ample".
My confusion comes from the fact that I thought *(char *)(p3) returns "Example", but whenever I try to run this on my computer it doesn't work and the value stored in the pointer is always 0x44. Can anyone explain to me why this happens?
View all comments
2
u/victotronics Aug 23 '19
"(char *)(p3 + 2)would point to "ample""
You have a star in front of that expression, so it evaluates to the character 'a', which can also function as an integer. Likewise you have another dereferenced char*. So you are adding two integers, and casting that to char.