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

void* question

P: n/a
What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?
Thanx
Nov 14 '05 #1
Share this Question
Share on Google+
26 Replies


P: n/a
Janice wrote:
What is the major reason for using void*?
Use a `void*' when you do not know the pointed-to
object's actual type, or do not care what it is, or
wish to conceal it.
When should we use void* for both input arguments and return value?
When both the arguments and the returned value meet
one or more of the criteria above.
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.

Perhaps if you'd give a little more detail about what's
puzzling you, I'd be able to explain more clearly.

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

Nov 14 '05 #2

P: n/a
Eric Sosman <er*********@sun.com> writes:
Janice wrote:
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #3

P: n/a
Eric Sosman <er*********@sun.com> spoke thus:
Perhaps if you'd give a little more detail about what's
puzzling you, I'd be able to explain more clearly.


IMHO it sounds like homework, so maybe you did just fine...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4

P: n/a
Ben Pfaff wrote:
Eric Sosman <er*********@sun.com> writes:

Janice wrote:
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.


C'mon, Ben: Leave her professor *some* excuse for taking
off a mark or two, won't you?

Janice: If your questions aren't homework, I repeat my
offer to try to answer more clearly if you'll explain your
difficulty in greater detail. If they *are* homework, you
can find the answers to these and to all your other C questions
in "The Annotated ANSI C Standard" by Herbert Schildt.

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

Nov 14 '05 #5

P: n/a
Janice wrote:
What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?

Do your own homework.


Brian
Nov 14 '05 #6

P: n/a
Eric Sosman wrote:
Ben Pfaff wrote:
Eric Sosman <er*********@sun.com> writes:
Janice wrote:
How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.

C'mon, Ben: Leave her professor *some* excuse for taking
off a mark or two, won't you?

Janice: If your questions aren't homework, I repeat my
offer to try to answer more clearly if you'll explain your
difficulty in greater detail. If they *are* homework, you
can find the answers to these and to all your other C questions
in "The Annotated ANSI C Standard" by Herbert Schildt.


Ouch, that hurts ;-)
I would not have thought of homework...

On the other hand, even though [ni]{2}famous, Schildt's book
costs something. I suggest reviewing the chapters 4-6 of
this newsgroup's FAQ. Start from here:
http://www.eskimo.com/~scs/C-faq/top.html
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #7

P: n/a
Eric Sosman wrote:
Janice wrote:
.... snip ...
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


Er - no cast needed nor desirable. Simply assign the void* to an
int* and the conversion is automatic.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

P: n/a
In <I8********@news.boeing.com> "Default User" <fi********@boeing.com.invalid> writes:
Janice wrote:
What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?


Do your own homework.


If it's homework, it's the most stupid kind of homework I have ever seen.
These are the things that have to be taught to the student, rather than
asking him to figure them out by himself.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9

P: n/a
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Eric Sosman wrote:
Janice wrote:

... snip ...
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


Er - no cast needed nor desirable. Simply assign the void* to an
int* and the conversion is automatic.


At the expense of creating an additional object. Sometimes it is worth
it, but not always.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #10

P: n/a
In <87************@benpfaff.org> Ben Pfaff <bl*@cs.stanford.edu> writes:
Eric Sosman <er*********@sun.com> writes:
Janice wrote:
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.


Bullshit! Of course such conversions require casts because this is how
explicit conversions are performed in C.

It is assignments between void pointers and other pointer types that do
not require casts, because the conversion is *implicitly* performed by the
assignment operator.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #11

P: n/a
Dan Pop wrote:
In <I8********@news.boeing.com> "Default User"
<fi********@boeing.com.invalid> writes:
Janice wrote:
What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?


Do your own homework.


If it's homework, it's the most stupid kind of homework I have ever
seen. These are the things that have to be taught to the student,
rather than asking him to figure them out by himself.

They don't sound like a list of questions that a newbie would come up
with to me. Maybe they do to you. To me they sound exactly like those
lists that instructors give to get the students to read a section of
text. That is, each question builds on the previous one.


Brian
Nov 14 '05 #12

P: n/a
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
Eric Sosman <er*********@sun.com> writes:
Janice wrote:

How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.


Bullshit! Of course such conversions require casts because this
is how explicit conversions are performed in C.

It is assignments between void pointers and other pointer types
that do not require casts, because the conversion is *implicitly*
performed by the assignment operator.


You, of course, are in the habit of making such conversions and not
assigning the result anywhere? I suspect the only place you could
gainfully do this is as an operand for sizeof. Your article is
well couched to allay any doubts some newbie may have on the
subject of void* usage.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #13

P: n/a


CBFalconer wrote:
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
Eric Sosman <er*********@sun.com> writes:

Janice wrote:

>How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.

This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.
Bullshit! Of course such conversions require casts because this
is how explicit conversions are performed in C.

It is assignments between void pointers and other pointer types
that do not require casts, because the conversion is *implicitly*
performed by the assignment operator.

You, of course, are in the habit of making such conversions and not
assigning the result anywhere?


Think of arguments for variadic functions -- I would rather
cast the void * (to an int object) to (int *) if I "claim" to
pass an int * than assign the value of the void * to an
additional variable which goes into the argument list...
I suspect the only place you could
gainfully do this is as an operand for sizeof.
Nope.
Your article is
well couched to allay any doubts some newbie may have on the
subject of void* usage.


True, Dan concentrated on one aspect but to be honest: The OP
and newbies who have not taken the hint I gave elsethread (to
read the FAQ) should in a discussion where answering the OP's
question in a homework-compatible way is avoided like hell be
well aware that they might not get the most helpful answer...
-Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #14

P: n/a
Da*****@cern.ch (Dan Pop) writes:
In <87************@benpfaff.org> Ben Pfaff <bl*@cs.stanford.edu> writes:
Eric Sosman <er*********@sun.com> writes:
Janice wrote:
How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.


Bullshit! Of course such conversions require casts because this is how
explicit conversions are performed in C.


It is not usually necessary to perform explicit conversions
between void * and pointers to object or incomplete type.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #15

P: n/a
Da*****@cern.ch (Dan Pop) writes:
In <I8********@news.boeing.com> "Default User"
<fi********@boeing.com.invalid> writes:
Janice wrote:
What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?


Do your own homework.


If it's homework, it's the most stupid kind of homework I have ever seen.
These are the things that have to be taught to the student, rather than
asking him to figure them out by himself.


Has it occurred to you that the students might have already been given
the necessary information (or that it might be in the textbook), and
that the point of the homework is for them to demonstrate that they
know it or can look it up?

Has it occurred to you to spend a moment thinking before throwing
words like "stupid" around?

Engage your brain, Dan.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16

P: n/a
Da*****@cern.ch (Dan Pop) writes:
In <87************@benpfaff.org> Ben Pfaff <bl*@cs.stanford.edu> writes:
Eric Sosman <er*********@sun.com> writes:
Janice wrote:
How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.


Bullshit! Of course such conversions require casts because this is how
explicit conversions are performed in C.

It is assignments between void pointers and other pointer types that do
not require casts, because the conversion is *implicitly* performed by the
assignment operator.


What exactly is wrong with Ben's statement? It looks entirely correct
to me.

Your statements appear to be correct as well, if you drop the
"Bullshit!".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #17

P: n/a
Da*****@cern.ch (Dan Pop) writes:
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Eric Sosman wrote:
Janice wrote:

... snip ...

How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


Er - no cast needed nor desirable. Simply assign the void* to an
int* and the conversion is automatic.


At the expense of creating an additional object. Sometimes it is worth
it, but not always.


The original question was "How can we cast ...". I suspect the intent
was "How can we convert ...", and that whoever wrote the question fell
into the common trap of using the word "cast" (which refers only to an
explicit cast operator) to refer to a conversion (which is an action
that occurs during execution of the program).

Given a literal interpretation of the question, the only answer to
"How can we cast ..." is "With a cast operator".

But in most cases where you want to convert a void* pointer to some
pointer type, you're going to be assigning it anyway, or passing it as
a function argument. I can't think of a case where it makes sense to
create an "additional object" that wasn't there already.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #18

P: n/a
Michael Mair wrote:
CBFalconer wrote:
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
.... snip ...
It is assignments between void pointers and other pointer types
that do not require casts, because the conversion is *implicitly*
performed by the assignment operator.


You, of course, are in the habit of making such conversions and not
assigning the result anywhere?


Think of arguments for variadic functions -- I would rather
cast the void * (to an int object) to (int *) if I "claim" to
pass an int * than assign the value of the void * to an
additional variable which goes into the argument list...


Such arguments are immediately assigned to the appropriate nameless
parameter.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #19

P: n/a


CBFalconer wrote:
Michael Mair wrote:
CBFalconer wrote:
Dan Pop wrote:

Ben Pfaff <bl*@cs.stanford.edu> writes:

... snip ...
It is assignments between void pointers and other pointer types
that do not require casts, because the conversion is *implicitly*
performed by the assignment operator.

You, of course, are in the habit of making such conversions and not
assigning the result anywhere?


Think of arguments for variadic functions -- I would rather
cast the void * (to an int object) to (int *) if I "claim" to
pass an int * than assign the value of the void * to an
additional variable which goes into the argument list...


Such arguments are immediately assigned to the appropriate nameless
parameter.


Umh, I am not sure what you mean. Maybe there is a
misunderstanding. Say, we have the following "program":

___________________________________________
#include <stdarg.h>
#include <stdio.h>

int foo (int is_intptr, void *ptr);
int bar (int howmany, ...);

int main (void)
{
int baz = 77;

printf("foo: %d\t", foo(1, &baz));
printf("baz: %d\n", baz);

return 0;
}

int foo (int is_intptr, void *ptr)
{
if (is_intptr)
return bar(1,(int *) ptr); /*<---*/
else
return -1;
}

int bar (int howmany, ...)
{
int m, *p;
va_list args;

va_start(args, howmany);

m = 0;
while (0 < howmany--) {
p = va_arg(args, int *);
m += *p - *p%42;
*p |= m;
}
va_end(args);

return m&0x42;
}
___________________________________________

Would you leave out the cast at the marked line in foo()?
If yes: What if the representations of (void *)&baz and
(int *)&baz were different?
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #20

P: n/a
Michael Mair wrote:
CBFalconer wrote:
Michael Mair wrote:
CBFalconer wrote:
Dan Pop wrote:
> Ben Pfaff <bl*@cs.stanford.edu> writes:
>

... snip ...
> It is assignments between void pointers and other pointer types
> that do not require casts, because the conversion is *implicitly*
> performed by the assignment operator.

You, of course, are in the habit of making such conversions and not
assigning the result anywhere?

Think of arguments for variadic functions -- I would rather
cast the void * (to an int object) to (int *) if I "claim" to
pass an int * than assign the value of the void * to an
additional variable which goes into the argument list...


Such arguments are immediately assigned to the appropriate nameless
parameter.


Umh, I am not sure what you mean. Maybe there is a
misunderstanding. Say, we have the following "program":

___________________________________________
#include <stdarg.h>
#include <stdio.h>

int foo (int is_intptr, void *ptr);
int bar (int howmany, ...);

int main (void)
{
int baz = 77;

printf("foo: %d\t", foo(1, &baz));
printf("baz: %d\n", baz);

return 0;
}

int foo (int is_intptr, void *ptr)
{
if (is_intptr)
return bar(1,(int *) ptr); /*<---*/
else
return -1;
}

int bar (int howmany, ...)
{
int m, *p;
va_list args;

va_start(args, howmany);

m = 0;
while (0 < howmany--) {
p = va_arg(args, int *);
m += *p - *p%42;
*p |= m;
}
va_end(args);

return m&0x42;
}
___________________________________________

Would you leave out the cast at the marked line in foo()?
If yes: What if the representations of (void *)&baz and
(int *)&baz were different?


No, but that is because bar is variadic, and ALL parameter values
have to be of the type expected. There is no parameter type here
to specify the conversion.

I guess I spoke too soon in respect to variadic functions. But not
with respect to properly prototyped normal functions. After
pointers, variadic functions are one of the largest sources of
error in C programming.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #21

P: n/a
On 9 Dec 2004 12:49:37 GMT,
Dan Pop <Da*****@cern.ch> wrote
in Msg. <cp**********@sunnews.cern.ch>
In <I8********@news.boeing.com> "Default User" <fi********@boeing.com.invalid> writes:
Janice wrote:
What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?
Do your own homework.


If it's homework, it's the most stupid kind of homework I have ever seen.


It sounds 100% like homework.
These are the things that have to be taught to the student, rather than
asking him to figure them out by himself.


I'm sure an attempt has been made to teach these things to Janice, but
since the effort seems to have been made in vain we're now treated to
homework questions.

--Daniel
Nov 14 '05 #22

P: n/a
On Wed, 8 Dec 2004 19:26:32 UTC, Eric Sosman <er*********@sun.com>
wrote:
Janice wrote:
What is the major reason for using void*?


Use a `void*' when you do not know the pointed-to
object's actual type, or do not care what it is, or
wish to conceal it.
When should we use void* for both input arguments and return value?


When both the arguments and the returned value meet
one or more of the criteria above.
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


No. You can simple assign a void* to any other pointer type:

int *p_i;
void *pv;
....
p_i = pv;
pv = p_i;

NO cast needed.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #23

P: n/a
On Wed, 8 Dec 2004 20:27:49 UTC, Eric Sosman <er*********@sun.com>
wrote:
Ben Pfaff wrote:
Eric Sosman <er*********@sun.com> writes:

Janice wrote:

How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.


C'mon, Ben: Leave her professor *some* excuse for taking
off a mark or two, won't you?

Janice: If your questions aren't homework, I repeat my
offer to try to answer more clearly if you'll explain your
difficulty in greater detail. If they *are* homework, you
can find the answers to these and to all your other C questions
in "The Annotated ANSI C Standard" by Herbert Schildt.

Urgs. Don't point to crap like this. Schildt knows nothing about the
standard! The only the book WAS good for was that it printed out the
old standard for less one had to pay for the original - but any word
oh him is crap and has nothing to do with the thing he means he
annostated.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #24

P: n/a
Herbert Rosenau wrote:
On Wed, 8 Dec 2004 19:26:32 UTC, Eric Sosman <er*********@sun.com>
wrote:

Janice wrote:
[...]
How can we cast the void* pointer to the type we need?


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


No. You can simple assign a void* to any other pointer type:

int *p_i;
void *pv;
...
p_i = pv;
pv = p_i;

NO cast needed.


The question was "How can we cast," not "How can we
convert" or "How can we assign." I gave an exact and
literal answer rather than a possibly more helpful one
because I suspected homework.

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

Nov 14 '05 #25

P: n/a
Herbert Rosenau wrote:
On Wed, 8 Dec 2004 20:27:49 UTC, Eric Sosman <er*********@sun.com>
wrote:

Janice: If your questions aren't homework, I repeat my
offer to try to answer more clearly if you'll explain your
difficulty in greater detail. If they are homework, you
can find the answers to these and to all your other C questions
in "The Annotated ANSI C Standard" by Herbert Schildt.

Urgs. Don't point to crap like this. Schildt knows nothing about the
standard!

He knows that.


Brian
Nov 14 '05 #26

P: n/a
Eric Sosman <er*********@sun.com> wrote:
Herbert Rosenau wrote:
On Wed, 8 Dec 2004 19:26:32 UTC, Eric Sosman <er*********@sun.com>
wrote:
Janice wrote:
How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.


No. You can simple assign a void* to any other pointer type: NO cast needed.


The question was "How can we cast," not "How can we
convert" or "How can we assign." I gave an exact and
literal answer rather than a possibly more helpful one
because I suspected homework.


Ordinarily, I'd agree with you. In this case, however, we're dealing
with casts. The OP might end up being yet another of the legion who
really do believe that you need to cast left, right and centre, and in a
few years' time we'll have to deal with malloc()-caster number thatmuch.

Richard
Nov 14 '05 #27

This discussion thread is closed

Replies have been disabled for this discussion.