One of the tricky question in interview comes out is
How do you point to the third byte of an integer. Assumption that integer is 4 byte. Here is the code to do the same
The concept is to have a void pointer to cast the integer pointer to the character pointer and increment that to point to the any byte location you wish to by simply incrementing it. Assumption char pointer is 1 byte.
How do you point to the third byte of an integer. Assumption that integer is 4 byte. Here is the code to do the same
Code: CPP
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int i = 35; //integer variable
int *ip = &i; //Integer pointer
void *vp = (int*)ip; //Void pointer for type casting
char *cp = (char*)vp; //Char pointer
cp++; //Point to second byte
cp++; //Point to third byte
return 0;
}

