What is the use of a void* pointer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zeeshan708
    New Member
    • Aug 2009
    • 25

    What is the use of a void* pointer?

    What is the use of a void* pointer? It points to a thing that is void ?
    So does that mean it is undefined or what? and also if someone can guide me on the following?


    After each input, the stream extraction operator returns a reference to the stream object that received the extraction message (e.g., cin in the expression cin >> grade). If that reference is used as a condition (e.g., in a while statement's loop-continuation condition), the stream's overloaded void * cast operator function is implicitly invoked to convert the reference into a non-null pointer value or the null pointer based on the success or failure of the last input operation. A non-null pointer converts to the bool value TRue to indicate success and the null pointer converts to the bool value false to indicate failure. When an attempt is made to read past the end of a stream, the stream's overloaded void * cast operator returns the null pointer to indicate end-of-file.
    from Dietel fiftth edition C++ How To Program
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    A void pointer is a pointer to any type. Such a pointer can be cast to any particulare type and a pointer to any particular type can be cast again to a void pointer.

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      void* are usually only found in C due to the lack of function overloading.

      A void* in C++ is a design weakness since the target type of the cast must be hard-coded which destroys your ability to reuse the code polymorphically .

      Comment

      • Troy Martin
        New Member
        • Aug 2009
        • 3

        #4
        The type void* is, as JosAH said, a pointer to any type. To be more accurate, it is a pointer to no type at all, as it is just a pointer. In order to use a void pointer, you must use either an explicit or (eech) an implicit cast.

        One of the advantages of void pointers are the fact you may cast it to anything. you may treat a void* as an int* on one line and an unsigned char* on the next.

        --Troy

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          As mentioned in reply #3 by weaknessforcats , the details of how you can use void pointers is different in C and C++. In C there is always an implicit cast to the appropriate target type when an assignment is made to or from a void pointer. This implicit cast does not occur in C++; it is your duty to provide the appropriate explicit cast.

          This means that a valid C program that uses void pointers will probably have compiler errors when passed through a C++ compiler.

          Personally, I consider this implicit cast one of the most appealing feature of void pointers in my C code.

          A void pointer cannot be dereferenced (because the compiler doesn't know what type is being pointed to). This suggests another use for void pointers ... they can be used in a C program to pass opaque object handles to and from a subroutine library. The library API casts object pointers into void pointers before returning them to the caller; and casts input void pointers from the caller into object pointers for subsequent use in the library function. The object type can then be kept hidden from the caller. There are other ways to do this in C (such as an incomplete structure). I'm sure C++ has smarter ways to accomplish this.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            By the way, a void pointer is a data pointer. You ought to keep data and function pointers separate.

            For example,
            • Do not assign a function pointer value to a void pointer.
            • Do not use the NULL macro with function pointers.

            Comment

            Working...