![]() |
| | #1 | ||
| C\C++ beginner Join Date: Nov 2007 Location: Somewhere nearby,Who Cares?
Posts: 375
| what are void pointers? 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:
Quote:
Last edited by Masterx; 11-04-2009 at 12:54 PM. | ||
| Masterx is offline | |
| | #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:
| |
| Elysia is offline | |
| | #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 | |
| | #4 | ||
| C\C++ beginner 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:
Quote:
| ||
| Masterx is offline | |
| | #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;
__________________ 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:
| |
| Elysia is offline | |
| | #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 | |
| | #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 | |
| | #8 | |||
| C\C++ beginner Join Date: Nov 2007 Location: Somewhere nearby,Who Cares?
Posts: 375
| thank you all . but would some one explain this part abit more ? Quote:
__________________ Highlight Your Codes Quote:
Quote:
| |||
| Masterx is offline | |
| | #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:
| |
| anon is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |