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
26 2133
In <87************ @benpfaff.org> Ben Pfaff <bl*@cs.stanfor d.edu> writes: Eric Sosman <er*********@su n.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
Dan Pop wrote: In <I8********@new s.boeing.com> "Default User" <fi********@boe ing.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
Dan Pop wrote: Ben Pfaff <bl*@cs.stanfor d.edu> writes: Eric Sosman <er*********@su n.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
CBFalconer wrote: Dan Pop wrote:
Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Eric Sosman <er*********@su n.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. Da*****@cern.ch (Dan Pop) writes: In <87************ @benpfaff.org> Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Eric Sosman <er*********@su n.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 Da*****@cern.ch (Dan Pop) writes: In <I8********@new s.boeing.com> "Default User" <fi********@boe ing.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_Keit h) 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. Da*****@cern.ch (Dan Pop) writes: In <87************ @benpfaff.org> Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Eric Sosman <er*********@su n.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_Keit h) 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. Da*****@cern.ch (Dan Pop) writes: In <41************ ***@yahoo.com> CBFalconer <cb********@yah oo.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_Keit h) 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.
Michael Mair wrote: CBFalconer wrote: Dan Pop wrote: Ben Pfaff <bl*@cs.stanfor d.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
CBFalconer wrote: Michael Mair wrote:
CBFalconer wrote:
Dan Pop wrote:
Ben Pfaff <bl*@cs.stanfor d.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
misunderstandin g. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kwan Ting |
last post by:
The_Sage, I see you've gotten yourself a twin asking for program in
comp.lang.c++ .
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1
If you the oh so mighty programmer that you pretend to be, why don't you
just write some? (And oh, void main is still not allow by the C++ standard.)
Seeming as how you tried to quote the standard in an attempt to pretend
you're right, I'll quote the standard to once...
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: sunglo |
last post by:
My doubt comes from trying to understand how thread return values work
(I know, it's off topic here), and I'm wondering about the meaning of
the "void **" parameter that pthread_join expects (I think this is
topical, since it's a C question, but please correct me and apologies
if I'm wrong).
I suppose that it's this way to allow for "generic" pointer
modification, but this implies dereferencing the "void **" pointer to
get a "void *"...
|
by: Twister |
last post by:
Hi All,
I have a question which might sound very basic.
I have a simple structure:
struct simple{
void *buffer;
};
typedef struct simple Simple;
|
by: Andreas Klimas |
last post by:
hello,
no many words, I will start with an example instead
int foo(void *r) {...}
void test(void) {
int *x=0;
foo(x); /* seems to me as valid */
}
| |
by: subramanian100in |
last post by:
I saw this question from www.brainbench.com
void *ptr;
myStruct myArray;
ptr = myArray;
Which of the following is the correct way to increment the variable
"ptr"?
Choice 1 ptr = ptr + sizeof(ptr);
|
by: Zero |
last post by:
Hello all,
I wonder about void?
To which category in the C programming language does it belong?
Of how many bits consits void?
Is it possible to define a varibale called
void a;
|
by: Barry |
last post by:
The problem brought by one earlier post from comp.lang.c++
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/
I take part of the question and reproduce the code to represent (partly)
the question.
#include <iostream>
template <class>
void f(...)
|
by: junky_fellow |
last post by:
Guys,
Consider a function
func(void **var)
{
/* In function I need to typecast the variable var as (int **)
I mean to say, I need to access var as (int **)
}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |