Basically I would like to store the address of a pointer in a buffer. Don’t ask me why
char * buff = "myBuff";
char * myData = (char*)malloc(sizeof(char*));
int addressOfArgTwo = (unsigned int)buff;
memcpy(myData, &addressOfArgTwo, sizeof(char*));
cout << "Int Val: " << addressOfArgTwo << endl;
cout << "Address in buffer:" << (unsigned int)*myData << endl;
I can’t see why the above code doesn’t work. It outputs:
Int Val: 4472832
Address in buffer:0
When the Int Val & Address in Buffer should be the same. thanks
You dereference a char *, resulting in a char, and then cast that 1-byte char to an int, not the entire 4 bytes of address (if this is a 32-bit machine, 8 bytes on 64-bit). 4472832 is 444000 in hexadecimal. On a little-endian machine, you grab that last 00.
should result in the correct number being displayed.