473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void* question

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
Michael Mair wrote:
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?


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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #21
On 9 Dec 2004 12:49:37 GMT,
Dan Pop <Da*****@cern.c h> wrote
in Msg. <cp**********@s unnews.cern.ch>
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.


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
On Wed, 8 Dec 2004 19:26:32 UTC, Eric Sosman <er*********@su n.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
On Wed, 8 Dec 2004 20:27:49 UTC, Eric Sosman <er*********@su n.com>
wrote:
Ben Pfaff wrote:
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.


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
Herbert Rosenau wrote:
On Wed, 8 Dec 2004 19:26:32 UTC, Eric Sosman <er*********@su n.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
Herbert Rosenau wrote:
On Wed, 8 Dec 2004 20:27:49 UTC, Eric Sosman <er*********@su n.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
Eric Sosman <er*********@su n.com> wrote:
Herbert Rosenau wrote:
On Wed, 8 Dec 2004 19:26:32 UTC, Eric Sosman <er*********@su n.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
9008
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...
188
17428
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
7
2489
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 *"...
31
3867
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;
4
2089
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 */ }
53
4824
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);
10
1719
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;
2
2471
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(...)
28
1824
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 **) }
0
9423
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,...
0
10216
Oralloy
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...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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,...
0
8873
agi2029
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...
0
6675
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();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
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...

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.