Thread: dereferencing void pointer

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    134

    dereferencing void pointer

    Hi,

    I have a void pointer. I am mallocing to it and cast it to appropriate type. However I am getting compiler errors about dereferecing void pointer.

    Following is the code

    Code:
    /*
    typedef struct pptp_generic_t_
    {
      CARD16 length;
      CARD16 pptp_msg_type;
      CARD32 cookie;
      CARD16 control_message_type;
      CARD16 reserved0;
    } pptp_generic_t;
    
    typedef struct ctrl_msg_sccrq_t_
    {
      pptp_generic_t pptp_generic_pkt;
      CARD16 version;
      CARD16 reserved1;
      CARD32 framing;
      CARD32 bearer;
      CARD16 channels;
      CARD16 firmware;
      CARD8 hostname[64];
      CARD8 vendor[64];
    } ctrl_msg_sccrq_t;
    */
    void *packet;
    
    if (ptype == START_CONTROL_CONNECTION_REQUEST)
    {
      packet = (ctrl_msg_sccrq_t *)malloc(sizeof(ctrl_msg_sccrq_t));
      packet->pptp_generic_pkt.pptp_msg_type=CONTROL_MESSAGE;
      packet->pptp_generic_pkt.cookie=MAGIC_COOKIE;
       
    packet->pptp_generic_pkt.control_message_type=START_CONTROL_CONNECTION_REQUEST;
        packet->pptp_generic_pkt.reserved=0;
    
    
    }
    Compiler errors

    pclientfunction.c: In function `sendPpacket':
    pclientfunction.c:116: warning: dereferencing `void *' pointer
    pclientfunction.c:116: request for member `pptp_generic_pkt' in something not a structure or union
    pclientfunction.c:117: warning: dereferencing `void *' pointer
    pclientfunction.c:117: request for member `pptp_generic_pkt' in something not a structure or union
    pclientfunction.c:118: warning: dereferencing `void *' pointer
    pclientfunction.c:118: request for member `pptp_generic_pkt' in something not a structure or union
    pclientfunction.c:119: warning: dereferencing `void *' pointer
    pclientfunction.c:119: request for member `pptp_generic_pkt' in something not a structure or union
    make: *** [pptpclient] Error 1
    Can anybody help me whats going wrong here?

    Thanks,

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void *packet;
    
    packet->pptp_generic_pkt.pptp_msg_type=CONTROL_MESSAGE;
    As the compiler gently told you, you cannot dereference a void pointer simply cause it has no type. Cast it to a packet pointer before using the -> operator:
    Code:
    void* packet = ...
    ctrl_msg_sccrq_t* packet2 = reinterpret_cast<ctrl_msg_sccrq_t*>(packet);
    Why are you using void pointers anyway?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Hm, C forum, not C++, use this instead:
    Code:
    (ctrl_msg_sccrq_t*)packet;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    but I thought, I casted it properly before dereferencing it.

    Code:
    packet = (ctrl_msg_sccrq_t *)malloc(sizeof(ctrl_msg_sccrq_t));
    is it not correct ? Pls tell me.

    Thanks,

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Read the FAQ.

    Malloc returns void* and if you cast it to ctrl_msg_sccrq_t * it will be assigned to packet and since packet is type void* you really done nothing. Magos suggested right way...

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  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