Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C/C++ - I'm trying to understand void..

Author
Message
spine
17
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 1st Nov 2009 23:31 Edited at: 1st Nov 2009 23:34
Okay, I've read two c++ tutorials to fully understand what void is. They both say:
~Void can point to any data type~
~Void cannot be dereferenced~

So in other words, that means it can point to something, but it can't retrieve the value?

Also, could someone explain the second void-the one in parentheses-and why it's sometimes replaced with another value? I'd very much appreciate it if the explanation isn't technical.

Thank you!

EDIT: I forgot to make it clear that this was a C++ question.. can someone fix that?
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 2nd Nov 2009 12:41 Edited at: 2nd Nov 2009 12:41
The keyword void means basically 'no type'. If you specify a function's return type as void it means it doesn't return a value.

A void pointer is different. You can assign the address of any type of variable to a void pointer.
puppyofkosh
19
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 2nd Nov 2009 21:52
by the "void in the parentheses", do you mean like this?

spine
17
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 3rd Nov 2009 01:36
Yes, that one. I understand now; I misread my C++ tutorial. Thanks for clearing that up, Benjamin!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Nov 2009 12:47
Quote: "do you mean like this?"

Yes, that's what he means. In C++ it's not actually needed and you can remove it.

Quote: "You can assign the address of any type of variable to a void pointer."

Think of a void pointer as an address holder - to do anything with it you have to at minimum convert it to a pointer of the type you require.

NeX the Fairly Fast Ferret
21
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 5th Nov 2009 20:42
Void is nothing. If a function has void as parameters or returns void, it has nothing as parameters or returns nothing.

Void *, on the other hand, is also nothing. Well, a pointer to nothing. It's meaningless. You can't do anything with it without typecasting it to something else. It's a valid pointer just like any other pointer, but C doesn't know what's on the other end. You've got to tell it by typecasting:



Athlon64 2.7gHz->OC 3.9gHz, 31C, MSi 9500GT->OC 1gHz core/2gHz memory, 48C, 4Gb DDR2 667, 500Gb Seagate + 80Gb Maxtor + 40Gb Maxtor = 620Gb, XP Home
Air cooled, total cost £160

Login to post a reply

Server time is: 2026-06-10 03:31:40
Your offset time is: 2026-06-10 03:31:40