Thread: void* problem

  1. #1

    void* problem

    I'm building a data grid control.

    It's struct which is I set in SetWindowLong is the MITH32GRIDSTRUCT struct.

    Code:
    typedef struct{
       LPSTR lpText,lpValue;
       int id,iIndex,iNumObjects,iType,iValue;
       bool bState,bValue;
       void *objects;
       RECT rc;
     }GRIDOBJECT;
     
     typedef struct{
       GRIDOBJECT*stems;
       int iNumStems;
       bool bInit,bSet;
       HICON *icons;
     }MITH32GRIDSTRUCT,*lpMITH32GRIDSTRUCT;
    Anyway, as you can see the grid has an array of GRIDOBJECT (stems)..

    I needed GRIDOBJECT to have an array of GRIDOBJECT, I could have made an identical struct but I thought I could use void* for it.. However now when it comes time to size the rects of each item in the grid (which I handle on WM_SIZE) I have a problem..
    Code:
         void size( GRIDOBJECT* object, int index, int type, RECT rc ){
         	
         	int j = index;
         	while( j < (GRIDOBJECT)object->objects[index]->iNumObjects ){
         	  
         	  if( object->objects[j]->iType == 1 ){
         		object->objects[j]->rc.top = rc.bottom;
         		object->objects[j]->rc.bottom += rc.bottom-rc.top;
         		object->objects[j]->rc.left = rc.left;
         		object->objects[j]->rc.right = rc.right;
         	  }else{
         		object->objects[j]->rc.top = rc.bottom;
         		object->objects[j]->rc.bottom = rc.bottom-rc.top;
         		object->objects[j]->rc.left = rc.left;
         		object->objects[j]->rc.right = rc.right;
         		size(&object->objects[j], 0, 1, rc);
         	  }
         	  j++;
         	}
         	
         }
    I call that for each of the GRIDOBJECT*stems in the MITH32GRIDSTRUCT struct. But I get an error because object->objects[] is a void* ...

    void* is not a pointer-to-object type
    What should I do?
    Last edited by Mithoric; 04-29-2004 at 03:11 PM. Reason: formatting issues in [code]

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You can't index a void pointer directly because it is unknown to the compiler what the heck it's pointing to. That's what it's crying about.
    Try casting it: ((LPRECT*)object->objects)[j]->rc...

  3. #3
    I've tried casting all sorts of types but I still get the error (MingW).

  4. #4
    This is the first line which gets the error --

    Code:
    while( j < object->objects[index]->iNumObjects ){
    I've tried -

    Code:
    while( j < (int)object->objects[index]->iNumObjects ){
    But to no avail.
    Last edited by Mithoric; 04-29-2004 at 03:39 PM.

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    There are a number of different ways to go about this. Using the code you have, modify your 'size' function as follows ( create a temporary pointer to objects member of GRIDOBJECT and use that to access RECT member):
    Code:
    void size( GRIDOBJECT* object, int index, int type, RECT rc ){
          
      GRIDOBJECT *go=(GRIDOBJECT*)object->objects;
      int j = index;
    
      while( j < go[index].iNumObjects ){
         	  
        if( go[j].iType == 1 ){
        go[j].rc.top = rc.bottom;
        go[j].rc.bottom += rc.bottom-rc.top;
        go[j].rc.left = rc.left;
        go[j].rc.right = rc.right;
        }else{
        go[j].rc.top = rc.bottom;
        go[j].rc.bottom = rc.bottom-rc.top;
        go[j].rc.left = rc.left;
        go[j].rc.right = rc.right;
        size(&go[j], 0, 1, rc);
        }
        j++;
        }
      }
    If you're compiling as c++ (which use of type 'bool' would seem to imply) then you should drop the 'typedef' from your struct definitions which would allow you to eliminate the void*, eg:
    Code:
    struct GRIDOBJECT_STRUCT{
      LPSTR             lpText,lpValue;
      int               id,iIndex,iNumObjects,iType,iValue;
      bool              bState,bValue;
      GRIDOBJECT_STRUCT *objects;
      RECT rc;
      };
     
    GRIDOBJECT_STRUCT GRIDOBJECT;
    You would obviously have to amend your other structure (MITH32GRIDSTRUCT) accordingly but at least you would be able to access the objects as you more or less originally intended.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    I don't know why I have ever been using typedef but I changed the struct and it works great (apart from the operand problem by using -> instead of .

    Thanks a bunch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM