I am trying to convert my struct into a char*, and then back to a struct. But I guess I am missing something. Once it is returned to struct, only one attribute of the struct is correct. The rest is all wrong. Here is my code.
#include <iostream>
using namespace std;
struct INFO {
unsigned char a;
int b;
int c;
char g[121];
}inf;
int main () {
char frame[128];
INFO test1 = INFO();
test1.a='y';
test1.b=4000;
test1.c=9000;
strcpy(test1.g, "Goodbye World");
sprintf(frame,(char*)&test1);
INFO test2 = INFO();
memcpy((char*)&test2, frame, sizeof(frame)); //shouldn't test2 have test1 values?
cout << test2.a<<"\n";
cout << test2.b<<"\n";
cout << test2.c<<"\n";
cout << test2.g<<"\n";
getchar();
return 0;
}
Output:
y
-858993460
-858993460
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
Only test2.a is correct. Am I converting it to a char* wrong, or is it the way I convert it back? Thanks