Hi, I think this problem should be pretty easy, but I can't find the answer anywhere. In my assignment we have to get the address of a private class member string.
We've been told to use sprintf, so
I should be able to save the string's address into an int
and then I can have sprintf put it into a string for me.
Hmm, well I haven't actually tried to compile this, but you could try making a pointer to the data and then storing the data address of the pointer to the string? No idea if it will actually work though...
And also, usually people don't respond in <1 hr...
EDIT: Another possible idea...try reading the data address into a stringstream, then making the string = the stringstream.
cool, I appreciate the comment and didnt mean for my last one to sound irritated lol.
This is the example that was laid out in the base program.
It's suggesting that the hex number would be an integer, I'm pretty sure at least. So, it would seem that somehow I should be able to save the address of a string into an integer...
// Example how to convert hexadecimal number
// (the tag address) to string
//sprintf_s(pg,"%x",hex number to convert);
I might be making this harder than it seems, since when I spoke to someone else about it, and didn't get the details, they said they pretty much just took out the "//" and it worked, but obviously the "hex number to convert" has to be a variable or some sort of value.
1.) The way you stated
2.) Change a hex number into a integer and store it to a string
For 1, I don't really have any more ideas.
For 2, you could just get the hex number and just store it to the string (IDR how it is actually stored though, so it could possible store 0xa to the string)...i.e.:
// Example how to convert hexadecimal number
// (the tag address) to string
//sprintf_s(pg,"%x",hex number to convert);
// where tag is the address I need saved
// the sprintf function was given by the professor and I'm pretty sure
// it takes an integer in that hex number position
string* temp;
*temp = tag;
int add;
add = *&temp; // this doesn't work either, it's just one combination that I
// keep going over.
// the string = hex didn't work either I'm afraid
// would appreciate any help, thanks again
i don't really think you need to do any hex converting, but either way, that's the easy part. also, there the code below should work to get the address... so here is a sample that may solve your problem:
1 2 3 4 5 6 7 8 9 10 11
#include <string>
// store the address in an int (notice the cast, to make sure the compiler isn't confused.)
int str_address = (int) &tag;
// now put the address in a string
char* add_str = newchar [ NEEDED_SIZE ];
sprintf ( add_str, "%x", str_address );
// print out the hexadecimal representation of the address
cout << "address: " << add_str;
that should do it. the code above would basically allow you to print out the address of the string object 'tag' in hex representation. since an int is 32 bits, that means 8 hex characters, so the code above should produce something similar to:
Ok there's no reason to go through an int at all. I'm not 100% sure, but I believe that the C++ standard only mandates that sizeof( void* ) == sizeof( int ), which would mean that all of the above code snippets are not portable and happen to work because most compilers will make non-member pointers the same size as int.
Agreed. While x86 and SPARC and many other architectures work fine, it is entirely possible for different pointer types to have different sizes and internal formats.
The only things the standard guarantees (IIRC) are:
sizeof( void* ) == sizeof( int ) [just like jsmith said], and
The integer zero is properly translated to the proper bit-pattern for a null-pointer of the given type on your hardware. (So that things like if (myPtr != NULL) work.)
Abuse of pointer/integer casting is common on x86 processors. It is less common on others.