• Advertisement

Archived

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

dereference problem

This topic is 6555 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

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!?

Share this post


Link to post
Share on other sites
Advertisement
If you have

void * data = new long;

Then you can do:

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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)))

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
I think I see now:

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

Correct?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement