Help on void *

D

#define

Hello,

Does any body know the advantage of writing function argument as void*
(rather than using int*, char* etc). what is the advantage or
disadvantage of this.
I've seen most of the library functions are implemented this was, as
they provide generic interface.

/*Function defination*/

int Msg_Edit(void *ptr)
{

return *((int*)ptr); //here i should know the type before hand as
int....


}


/* Function call */

Msg_Edit(&msg);


Now msg buffer can be of any type char, int, float....(Am i right)

Is there a way in way in C, by which i know the type of pointer passed
to the function on runtime.
otherwise i'll need to typecast ptr to a specific type (int*, char*
etc) which should be prior knownto me.

Then, what is the main advantage of this approach...

Best Regards,

#define.
 
E

Eric Sosman

#define wrote On 12/19/05 10:33,:
Hello,

Does any body know the advantage of writing function argument as void*
(rather than using int*, char* etc). what is the advantage or
disadvantage of this.

Disadvantage: A `void*' carries no information about
the actual type of the data it points to, so the function
needs to obtain that information in some other way.

Advantage: A `void*' carries no information about the
actual type of the data it points to, so a function that
doesn't need to know the type isn't burdened with trying
to figure it out.
I've seen most of the library functions are implemented this was, as
they provide generic interface.

Exactly. memcpy() doesn't need to know the type of
the data it's copying, so it can use `void*'. Without a
"generic" pointer of some kind, you'd need separate versions
of memcpy() for copying characters, shorts, ints, floats,
doubles, ... And, of course, for all the kinds of types a
programmer might invent: struct foo, union bar, ...

On the other hand, some functions *do* require specific
types, and they don't use `void*'. For example, strcpy()
is only for copying character strings -- not arrays of
`unsigned long', for example -- so it uses `char*' pointers.
/*Function defination*/

int Msg_Edit(void *ptr)
{

return *((int*)ptr); //here i should know the type before hand as
int....


}


/* Function call */

Msg_Edit(&msg);


Now msg buffer can be of any type char, int, float....(Am i right)

Yes.
Is there a way in way in C, by which i know the type of pointer passed
to the function on runtime.

There is nothing built-in. If you need to know the type
of some piece of data, you need to keep track of it yourself.
 
D

dukguru

in c++,
void FuncA(void *p)
{
if(typeid(*p) == typeid(int)) // RTTI check
{
// p is int pointer
}
}

// #include <typeinfo>

no way in c

==
(e-mail address removed)
 
D

Dave Thompson

in c++,
void FuncA(void *p)
{
if(typeid(*p) == typeid(int)) // RTTI check
{
// p is int pointer
}
}

// #include <typeinfo>
<OT> No, (even) in C++ the runtime/dynamic part of RTTI only works for
classes with virtual methods; 5.2.8[expr.typeid] 10.3[class.virtual].
typeid(*voidptr) is statically 'void' and never == that of int.

IIRC Icon does support type inquiry on all types, and maybe Eiffel.
And LISP, of course. <G>

- David.Thompson1 at worldnet.att.net
 

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,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top