How do you track type information when converting int* to void*

C

Chad

Keith made the following comment

" You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself. "

How would you track the type infornation?
 
N

Nick Keighley

Keith made the following comment
" You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself. "

How would you track the type infornation?

why are you casting a whatever-type to a void*?
If you need the type information why not keep the original type?

I suppose some sort of generic container might be a reason
to do this.

Basically you need some sort of tag

enum Tag {INT, FLOAT, STRING};

struct Node
{
enum Tag tag;
void *data;
};

store data like this

struct Node nod;
int i;
nod.tag = INT;
nod.data = &i;

add (&handle, &nod);

casting it back when you retrieve it.

You might consider a union instead. Or a redesign.
 
N

Nick Keighley

Nick Keighleysaid:


Where did he say he was casting?

a moment of insanity on my part

Because you're writing generic code?

ok, but does he need generic code? Sometimes things
get stuffed into void* for no good reason that I can see.
But I should assume Good Faith; that the OP is not in fact
crazy.

Ha - yes, quite so.


Why?

a *second* moment of insanity


--
Nick Keighley

"Almost every species in the universe has an irrational fear of the
dark.
But they're wrong- cos it's not irrational. It's Vashta Nerada."
The Doctor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top