Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help on void *

Reply
Thread Tools

Help on void *

 
 
#define
Guest
Posts: n/a
 
      12-19-2005
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.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-19-2005


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


--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
dukguru
Guest
Posts: n/a
 
      12-21-2005
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 Removed)

 
Reply With Quote
 
dukguru
Guest
Posts: n/a
 
      12-21-2005
in C++

you can check RTTI



no way in C

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      01-04-2006
On 20 Dec 2005 18:33:53 -0800, "dukguru" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
returning a void (*)(void) Sergio C++ 6 01-05-2005 08:30 PM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments