What does this line do exactly?
(char*) (&input)
I know that it converts the int(input) to char array..or am I wrong? and could you guide me how does it compute?
update:
I think I now understand a little, then I created c++ code from your comments
#include <cstdlib>
#include <iostream>
using namespace std;
int main(){
int input = 123456;
int *p = &input;
char *cp = (char*)p;
for(int counter = 0;counter <sizeof(input); counter++){
cout << *(cp+counter) << endl;
}
system("pause");
return 0;
}
but i think my code is wrong. because it only shows ‘@’ , big ‘r’? , and something like cross sign …
It’s just casting the address of
inputas a pointer to achar. You might see something slightly different like:Which is useful for reading single bytes at the address of
input, sincecharis a byte in size. Note thatcharis signed and ranges from -128 to 128 whileunsigned charis (obviously) unsigned and more appropriate for reading bytes as it’s unsigned and ranges from 0 to 255.You could then read specific bytes like:
Or perhaps you might want to set specific bytes such as: