C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2009, 12:52 PM   #1
C\C++ beginner
 
Masterx's Avatar
 
Join Date: Nov 2007
Location: Somewhere nearby,Who Cares?
Posts: 375
what are void pointers?

hello all .
well what are void pointers ? and how do we use them ? i mean where exactly do we need these kind of pointers , you know im kinda confused ! void means nothing! so whats the point of pointing to nothing! ?
can anyone tell me what fields utilize the void pointers ? some examples! would be greatly appreciated .
thank you all dears in advance

----
by saying void pointers i mean ' void * '
__________________
Highlight Your Codes

Quote:
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning."
Rich Cook

Quote:
"...a computer is a stupid machine with the ability to do incredibly smart things, while
computer programmers are smart people with the ability to do incredibly stupid things. They are,
in short, a perfect match.."
Bill Bryson

Last edited by Masterx; 11-04-2009 at 12:54 PM.
Masterx is offline   Reply With Quote
Old 11-04-2009, 01:04 PM   #2
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
They are pointers that can point to anything, basically. Do not use them in C++ unless you have to interface with some C code. Templates are a much better solution.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-04-2009, 01:07 PM   #3
Registered User
 
Join Date: Oct 2006
Posts: 263
a void pointer is essentially a type-less pointer. you cannot dereference a void pointer, but you can cast it to any other type of pointer. in this case void doesn't mean 'nothing', but rather 'unknown'. the compiler doesn't know what's at the address pointed to by a void pointer until you tell it what it is with a cast.
Elkvis is offline   Reply With Quote
Old 11-04-2009, 01:15 PM   #4
C\C++ beginner
 
Masterx's Avatar
 
Join Date: Nov 2007
Location: Somewhere nearby,Who Cares?
Posts: 375
thanks .
can some one by the way show me some examples of using void pointers ! i mean just in action, how do we use em! just by casting them to other kind of pointers ? if so why would we want to do that ? pointing to some where unknown !whats the point of making void pointers anyway! ?
__________________
Highlight Your Codes

Quote:
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning."
Rich Cook

Quote:
"...a computer is a stupid machine with the ability to do incredibly smart things, while
computer programmers are smart people with the ability to do incredibly stupid things. They are,
in short, a perfect match.."
Bill Bryson
Masterx is offline   Reply With Quote
Old 11-04-2009, 01:17 PM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Code:
int* p = new int;
void* p2 = p;
int* p3 = (int*)p2;

*p2 = 0; // Error
*p3 = 5; // OK
delete p;
void* pointers are usually used in C as a generic type. C++ has inherited it, but it has much better solutions right now (templates) than using void* pointers.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-04-2009, 02:32 PM   #6
3735928559
 
Join Date: Mar 2008
Posts: 662
to me, a void* a pointer to something that is supposed to be unknown to that particular object. e.g: a chunk of arbitrary or mixed-type binary data (such as data read as packed-binary such as an archive or off a TCP stream).
m37h0d is offline   Reply With Quote
Old 11-05-2009, 09:52 AM   #7
Registered User
 
Join Date: Apr 2006
Posts: 1,193
Arbitrary data is often referred to with a char pointer though, so that it can be read byte by byte.

In many cases polymorphism is used as the alternative to void pointers.

The special property of void pointers is that any pointer type, except function and method pointers, can be assigned to a void pointer. In C it also has the property that it can be assigned to any pointer without a cast.

A typical C use of void pointers is callback functions, where a generic function takes as arguments some other function via a function pointer and some data. The data is passed via a void pointer.
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
King Mir is offline   Reply With Quote
Old 11-07-2009, 09:18 AM   #8
C\C++ beginner
 
Masterx's Avatar
 
Join Date: Nov 2007
Location: Somewhere nearby,Who Cares?
Posts: 375
thank you all .
but would some one explain this part abit more ?
Quote:
A typical C use of void pointers is callback functions, where a generic function takes as arguments some other function via a function pointer and some data. The data is passed via a void pointer.
__________________
Highlight Your Codes

Quote:
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning."
Rich Cook

Quote:
"...a computer is a stupid machine with the ability to do incredibly smart things, while
computer programmers are smart people with the ability to do incredibly stupid things. They are,
in short, a perfect match.."
Bill Bryson
Masterx is offline   Reply With Quote
Old 11-07-2009, 10:18 AM   #9
The larch
 
Join Date: May 2006
Posts: 3,082
For example look at the standard C qsort function with the usage example.

qsort doesn't care what it is sorting, as it's just pushing bytes around and so it just feeds some addresses to the comparison function. The comparison function, on the other hand, should know what it is comparing and hence cast the void* back to the real type.
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 11-07-2009, 11:05 AM   #10
Registered User
 
Join Date: Apr 2006
Posts: 1,193
Another example is pthread_create() which is used to create threads on POSIX systems.

Sorting is generally implemented via template functions is C++, such as std::sort.

By contrast creating a thread would be better accomplished by a function taking a polymorphic object, as is done in java.
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
King Mir is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting a swf file in a windows application face_master Windows Programming 12 05-03-2009 11:29 AM
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
i cannot see the mouse arrow peachchentao C Programming 6 12-10-2006 04:14 AM
Quack! It doesn't work! >.< *Michelle* C++ Programming 8 03-02-2003 12:26 AM
oh me oh my hash maps up the wazoo DarkDays C++ Programming 5 11-30-2001 12:54 PM


All times are GMT -6. The time now is 01:49 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22