Thanks Grey Wolf for your response. The answer to why its a long, long story... :)
I am working with a 3D game engine and coding a world editor consisting from 3D meshes.
Each 3D mesh has its own memory handle, an INTEGER, and it is a protected member of a class name CActor.
During the creation of the 3D mesh I have the ability to store a string into mesh and I can retrieve this string having the INTEGER before as an argument.
When the user selects a 3D mesh with the mouse then my 3D engine returns the mesh INTEGER. I must write code to locate quickly which object is selected which means I have to locate the address of the proper CActor instance.
#include <stdio.h>
#include <iostream>
struct Test{
int a;
};
int main(int argc, char *argv[])
{
Test test;
Test *pTest;
pTest = &test;
pTest->a = 9;
char buffer [50];
sprintf(buffer, "%p", pTest);
Test *pTest2;
sscanf(buffer,"%x",&pTest2);
printf("the value of pTest is: %p\n", pTest);
printf("\tthe value of pTest->a is: %d\n", pTest->a);
printf("the value of buffer is: %s\n", buffer);
printf("the value of pTest2 is: %p\n", pTest2);
printf("\tthe value of pTest2->a is: %d\n", pTest2->a);
return 0;
}
I tested and it works, but I think it is not safe because the %x type in sscanf function reads an integer which is a smaller size than an instance address
Yea,
This is good for a 32bit application. As for the returned address type casting works both for VC and Mingw compilers. I mean that I got no error when coding:
#include <stdio.h>
#include <iostream>
struct Test{
int a;
};
int main(int argc, char *argv[])
{
Test test;
Test *pTest;
pTest = &test;
pTest->a = 9;
char buffer [50];
sprintf_s(buffer, "%p", pTest);
unsignedlong pTest2;
sscanf_s(buffer,"%x",&pTest2);
printf("the value of pTest is: %p\n", pTest);
printf("\tthe value of pTest->a is: %d\n", pTest->a);
printf("the value of buffer is: %s\n", buffer);
printf("the value of pTest2 is: %lu\n", pTest2);
printf("\tthe value of pTest2->a is: %d\n", ((Test *)pTest2)->a);
getchar();
return 0;
}
I don't see any issue any more.
Thanks Grey Wolf, you helped me!