Thread: void pointers

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    void pointers

    Hey I'm having trouble with some void pointers, I'm really pretty unclear on their usage. If some one could provide me with an explanation of them and how to use them, I would be mighty appreciative. Here is the function header for the function that uses the void*.

    wordList* antonym(char* word, void* thesaurus)
    The structures the I am using for this program are:

    typedef struct wordListItem {
    char* word;
    struct wordListItem* next;
    } wordList;

    typedef struct tableItem
    {
    char* Keyword;
    tableItem* next;
    wordList* Syn;
    wordList* Ant;
    } tableEntry;


    struct Hashtable
    {
    int size;
    int count;
    tableItem* Array;
    }Hash;

    Thesaurus will point to tHash which will point to an array of dynamically allocated tableItems, which have members wordlist* Syn and Ant. I want to return pointers to the Syn or Ant linked lists. Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    void* is the general purpose 'pointer to anything, without dereferencing' pointer.

    Any pointer can be cast into a void pointer (and back again) without loss of information (or warning from the compiler).

    The dereferencing bit is important, because you cant do
    ptr->
    (*ptr).
    ptr[]

    dereferencing operations on void pointers, you've got to cast the pointer back to a suitable type.

    From the look of it,
    struct Hashtable *tHash = thesaurus;

    As the first line of your antonym function seems like a good bet (you might need to tweak this, I'm not sure what your description means precisely).

    It's pretty easy to work out, just look at the type of pointer you call the function with in the first place.

    Does beg the question - why isn't thesaurus set to this type in the first place?

  3. #3
    Sayeh
    Guest
    the term 'void' is used by the compiler to allow syntax and gramatical checking without enforcing a specifc 'type'. It forces the compiler to satisfy the meaning of the code, without having to satisfy the letter.

    for example, if I create a function that excepts a 'void *', then I can pass it any kind of pointer. 'char *', 'long *', 'float *', it doesn't matter, the syntax check will pass.

    'void' literally means 'without definition'. meaningful but not specific.

    In it's simplest terms, you use 'void *' to tell the compiler that the variable will be an address, but you dont' know for what kind of object. This works because all addresses are the same size (at this time with a native 32-bit memory model).

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, 04:14 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM