void* question

J

Janice

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
 
E

Eric Sosman

Janice said:
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.
 
B

Ben Pfaff

Eric Sosman said:
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.
 
D

Default User

Janice said:
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
 
C

Christopher Benson-Manica

Eric Sosman said:
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...
 
E

Eric Sosman

Ben said:
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.
 
M

Michael Mair

Eric said:
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
 
C

CBFalconer

Eric said:
Janice wrote:
.... snip ...


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

Dan Pop

In said:
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
 
D

Dan Pop

In said:
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
 
D

Dan Pop

In said:
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
 
D

Default User

Dan said:
In <[email protected]> "Default User"


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
 
C

CBFalconer

Dan said:
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.
 
M

Michael Mair

CBFalconer said:
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
 
B

Ben Pfaff

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

Keith Thompson

In <[email protected]> "Default User"


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

Keith Thompson

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!".
 
K

Keith Thompson

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

CBFalconer

Michael said:
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.
 
M

Michael Mair

CBFalconer said:
... snip ...


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
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top