Advertisement

dereference problem

Started by March 16, 2000 06:30 PM
8 comments, last by vallis 24 years, 9 months ago
I have my Linked List class... I have my Node structs. They contain a void* pointer to the data. Now - how do I dereference that? The code looks like this: unsigned long Val = (unsigned long)List.Current->Data; but this is a pointer ---^^^^ And I need to dereference it....? Anyone - please!?
If you have

void * data = new long;

Then you can do:

long value = *((long *)data);
Advertisement
quote: Original post by SiCrane

If you have

void * data = new long;

Then you can do:

long value = *((long *)data);


Thats great - thanks! But do you know if there is any way to do it in one line? Without the extra declaration...


Edited by - vallis on 3/16/00 6:44:11 PM
quote: Original post by SiCrane

If you have

void * data = new long;

Then you can do:

long value = *((long *)data);


Thats great - thanks! But do you know if there is any way to do it in one line?
Well the type of *((long *)data) is a long, so just use that where ever you need to derefernce your void *.

Alternately you can just macro definition the conversions. ex:

#define DeRefAsLong(x) (*((long *)(x)))
Whoooooooosh!

That was the sound of something going way over my head

Would you be so kind as to explain that again for me SiCrane?

TIA!
Advertisement
Ok you''ve got a void * called data.
(long *)data is a pointer to a long.
*((long *)data) is a dereference of a pointer to a long.
So where ever you need derefernce that void * pointer as a long then you can put *((long *)data).

Alternately you can just create a macro that will take care of the dereferncing for you.
So if you have
#define DeRefAsLong(x) (*((long *)(data)))
Then you can use
long value = DeRefAsLong(data);

Or:

Some_Function_With_Long_Argument(DeRefAsLong(data));

but you can still do:

Some_Function_With_Long_Argument(*((long *)data));

Did that clear it up? Or am I just confusing you further?
I think I see now:

unsigned long Val = *((unsigned long *)List.Current->Data);

Correct?
Looks good, but as paranoia I always wrap both sides of a cast in paranthesis. i.e.
*((unsigned long *)(List.Current->Data))
rathar than
*((unsigned long *)List.Current->Data)
I just think it''s easier to read.
Brilliant!

Thanks for all your help SiCrane

This topic is closed to new replies.

Advertisement