Thread: What's the use of declaring a pointer to void ?

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    What's the use of declaring a pointer to void ?

    Hi,

    Why declare a pointer as void if it cannot be deferenced ?

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    6
    Its useful when you don't know for sure what type of data that pointer will be pointing to. For example, the fwrite function takes a void* argument so you will be able to write anything to the file:

    char buffer[MAX_BYTES];
    SOME_STRUCT str; // [edit: cant have a var name 'struct']

    fwrite(&buffer, sizeof(buffer), 1, fp);
    fwrite(&str, sizeof(str), 1, fp);

    also, you can dereference a void pointer if you want, just cast it into the desired type:

    void *p = ...;
    *(SOME_TYPE *)p; // cast p to a pointer to some type, then dereference
    Last edited by Duck-billed Pla; 01-15-2002 at 05:03 AM.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Nutshell, I think you are confusing it with setting a pointer to NULL.

    As Duck-billed Pla has shown, void only means undefined.....not unaccessable.....

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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Error, not sure what it means.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2005, 01:17 PM
  5. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM