473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help on void *

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
4 1376


#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
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
in C++

you can check RTTI

no way in C

Dec 21 '05 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sachin bond | last post by:
this code does not work.... plz help...... void breaker() {
15
by: Buck Rogers | last post by:
Hi guys! Task 1: Write a program which presents a menu with 5 options. The 5th option quits the program. Each option should execute a system command using system(). Below is my humble...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
0
by: Ben Harper | last post by:
I am stuck using VC7 right now, so I can't test this on 7.1, but I believe it is a bug in the compiler, although I hope it isn't. Please could someone confirm this for me, prove me wrong, or suggest...
9
by: silverchrono | last post by:
this is my first semester in C and im trying to figure out how to reset a counter. heres why im trying to do. void text() 59 printf("You can end entering the text by using '#'\n"); 60 int...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
4
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have:...
5
by: 16800960 | last post by:
I am stuck with this i need to declare the clrscr,pause and skip blanks functions like my code shows below # include <iostream> using namespace std; void clrscr (); //Prototype void pause...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.