what are void pointers?

This is a discussion on what are void pointers? within the C++ Programming forums, part of the General Programming Boards category; hello all . well what are void pointers ? and how do we use them ? i mean where exactly ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    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 * '
    Last edited by Masterx; 11-04-2009 at 11:54 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...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


  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    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.

  4. #4
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    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
    The Boost C++ Libraries (online Reference)

    "...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


  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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).

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    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.

  8. #8
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    thank you all .
    but would some one explain this part abit more ?
    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
    The Boost C++ Libraries (online Reference)

    "...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


  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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