Thread: Dereferencing a Void Pointer in a Structure

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    Dereferencing a Void Pointer in a Structure

    Hey all,

    Hopefully this is a simple question for someone with more void* experience than me.

    Say I have a data structure:

    Code:
    typedef struct sp_cntl_tag 
    {
    	void* pad;
    	uint8 type;
    	uint8 index;
    } sp_cntl_t
    and a function which takes a pointer to this structure as an input. Because of the 'type' variable, I know what the void pointer contains and would like to dereference it.

    How do I dereference a void* from a structure? My instinct was to do something like

    Code:
    sp_cntl_t* temp;
    desiredType = *temp->(typecast)pad;
    but this didn't work... suggestions?

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I do this for threads frequently and here is a little snippet.

    Code:
    struct s_data
    {
        SOCKET sck;
        char *host;
        char *ip;
        unsigned short port;
    };
    
    DWORD WINAPI s_listen (void *params)
    {
        struct s_data *data = (struct s_data *)params;
    
        printf("The host is %s\n", data->host);

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by simpsonseric View Post
    My instinct was to do something like

    Code:
    sp_cntl_t* temp;
    desiredType = *temp->(typecast)pad;
    but this didn't work... suggestions?
    Looks like you just put the cast in the wrong spot.
    Code:
    ...
    
    sp_cntl_t* temp = make_sp_cntl_t();
    
    ...
    
    if (temp->type == SUPER_TYPE)
    {
       super_type st = *(super_type*)temp->pad;
    }
    
    ...
    
    // or, breaking it down into individual actions:
    
    if (temp->type == SUPER_TYPE)
    {
       void *ptr = temp->pad;
       super_type *super_ptr = (super_type*)ptr;
       super_type st = *super_ptr;
    }
    
    ...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in C void pointer could be assigned to other pointer without cast
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You may also type-cast it before dereferencing:
    sp_cntl_t* temp;
    *((int*)temp->pad)

    Just remember that void* pointers cannot be dereferenced and must be cast to appropriate type before used in such a way.
    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
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM