By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,381 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,381 IT Pros & Developers. It's quick & easy.

Help on void *

P: n/a
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.

Dec 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


#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.
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.


--
Er*********@sun.com

Dec 19 '05 #2

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

// #include <typeinfo>

no way in c

==
du*****@gmail.com

Dec 21 '05 #3

P: n/a
in C++

you can check RTTI

no way in C

Dec 21 '05 #4

P: n/a
On 20 Dec 2005 18:33:53 -0800, "dukguru" <du*****@gmail.com> wrote:
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
Jan 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.