copying a pointers value

Started by
5 comments, last by bilstonwarrior 21 years, 2 months ago
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.
Advertisement
Doing it the ugly way...


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

u.vptr = NULL;
u.i = ~u.i;
//u.vptr holds what you want
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]
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
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).

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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]
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

This topic is closed to new replies.

Advertisement