Thread: Dereference pointer to void pointer to member

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    22

    Dereference pointer to void pointer to member

    Hello
    I've searched the board but can't work this one out.
    Trying to dereference pointer to void pointer to member.
    The program compiles without error until a put this line in.

    Code:
    Line 34: atest = pSllist->((FRUIT *)Object)->boxes;
    The compiler error is, Line 34: parse error before '(' token

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Hmm, could you post a bit more code?

    Specifically, your structure definitions, variable declarations, and even tossing in all lines of code before line 34 couldn't hurt.

    Something's tossing it off before that first bracket, but it's tough to figure out exactly what it could be without some more of the code.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    ok this my code
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <stddef.h>
    #include <stdio.h>
    #include "sllist.h"
    typedef  struct FRUIT {
           int boxes;
           int crates;
    }FRUIT;
    
    
    int main()
    {
      int atest;
      int result, tag;
      size_t size;
      SLLIST *pSllist = NULL;
      
      char c;
      FRUIT apples;
      FRUIT *papples;
      papples = &apples;
      apples.boxes = 5;
      apples.crates = 4;
      printf("apples boxes = %d\n",apples.boxes);
      printf("apples boxes = %d\n",papples->boxes);
    
    /* add apples to linked list */
      size = sizeof apples;
      result = SLAdd(&pSllist,tag,&papples,size);
      printf("result = %d\n",result);
      printf("pointer value %d \n",pSllist);
      atest = pSllist->((FRUIT *)Object)->boxes;
     
     
    
      return 0;
    }
    The function SLAdd is taken from a book c unleashed I don't really want to post unless you really need, anyway there is some error checking in the SLAdd function and it runs ok and afterwards pSllist no longer points to null.The SLAdd function takes my fruit object and uses a void *Object to malloc then copys my fruit object using memcpy.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Sorry there may have been a mistake in my call to the SLAdd function.
    I've changed
    Code:
    result = SLAdd(&pSllist,tag,&papples,size);
    to
    Code:
    result = SLAdd(&pSllist,tag,&apples,size);
    the code compiles without the dereference line.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > atest = pSllist->((FRUIT *)Object)->boxes;
    My guess is, it needs to be
    atest = ((FRUIT *)(pSllist->Object))->boxes;
    Cast the Object pointer (which is most likely a void*) into a FRUIT* and then dereference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Thank you Salem, that worked perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. void pointer to a member function
    By Geolingo in forum C++ Programming
    Replies: 7
    Last Post: 07-18-2003, 06:01 PM