• Advertisement

Archived

This topic is now archived and is closed to further replies.

copying a pointers value

This topic is 5513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i have a function that has to be layed out the following way: ReadFile(void *buffer) { fread(buffer,4,1,infile); } The problem is that i need to do this: buffer = ~buffer; but the "void" doesnt allow it. I cannot change it to char or anything as the varibale types that pass into the function MUST remain the same. i tried doing this: char tempbuf; fread(tempbuf,4,1,infile); tempbuf = ~tempbuf; buffer = &tempbuf; .... but it compiles but when i run it there are runtime errors. thanks for any help/advice. cheers Paul.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Doing it the ugly way...


union {
void* vptr;
int i;
}u;

u.vptr = NULL;
u.i = ~u.i;
//u.vptr holds what you want

Share this post


Link to post
Share on other sites
You know what you doing?

I wouldn't inverse the bits in a pointer unless I did it twice to nullify the effect. You probably want to inverse the bits in the data you've read, right? Then don't hesitate to use the dereferencing operator *

  
void ReadFile(void *buffer)
{
fread(buffer,4,1,infile);
assert(sizeof(int)==4); //since we read 4 bytes..

int* temp = reinterpret_cast<int*>(buffer);
*temp = ~(*temp);
}

zig

[edited by - civguy on January 22, 2003 12:30:56 PM]

Share this post


Link to post
Share on other sites
at the end of the function, "buffer" must equal ~ of the original one...

ie: 0110011 read from file.... into varibale "buffer"

at end of function "buffer" must equal 1001100


cheers

Paul

Share this post


Link to post
Share on other sites
ReadFile(void *buffer)
{
fread(buffer,4,1,infile);

unsigned int Integer = *(unsigned int *)buffer;
Integer = ~Integer;

Thats what you want. You dont'' want to inverse the pointer (buffer) you want to inverse what the pointer points to (*buffer).

Share this post


Link to post
Share on other sites
The data is not read into variable buffer. The buffer points to that data. Read my earlier reply and try it out

[edited by - civguy on January 22, 2003 12:59:44 PM]

Share this post


Link to post
Share on other sites
buffer is a pointer to an object of unknown type. If you reverse its bits, it''s not going to be a pointer to correct memory anymore... unless youre extremely lucky.

However, changing the memory pointed to by buffer is possible.

Your fread arguments tell me you''re reading a long integer (4 bytes), so you might consider a cast, like this :

*(unsigned long *)buffer = ~(*(unsigned long*)buffer));

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more

Share this post


Link to post
Share on other sites

  • Advertisement