Thread: void* ptr in structure?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    void* ptr in structure?

    is it legal to use a void* in a structure ?
    Q. What is a void* ?

    A void* is a generic pointer type. It's a pointer but the type pointed to is unknown. You may not dereference a void* but instead must first cast it to another pointer type before you dereference. Also you may not use a void* in any form of pointer arithmeti

    so i added it to a typedef
    Code:
    typedef enum {
     UCHAR=1,UINT,UDOUBLE,USTRING
    } UNIKIND;
    
    typedef struct _UNIVARLINK {
     UNIKIND type;
     int arrayc;
     char * name;
    
     struct _UNIVARLINK *next;
    
     //void*data;
    } UNIVAR;
    
    
    void createvar(UNIVAR**varstack,char * varname) 
    {
      UNIVAR*p;
    
      //make new var
      if(!(*varstack) || !(p=findvar((*varstack),varname)))
      {
       p = malloc(sizeof(UNIVAR*));
       p->name = malloc(strlen(varname)+1);
       strcpy(p->name,varname);
       p->type = 1;
       p->arrayc = 1;
       //p->data = NULL;
       p->next = (*varstack);
       (*varstack) = p;
       printf("\n\tCREATING VAR \'%s\'",(*varstack)->name);
      } 
      else
       printf("\n\tALREADY VAR \'%s\'",varname);
    }
    the first time i run createvar everything goes but if i uncomment //p->data = NULL;and //void*data; everything goes wrong
    My question : is it illegale to put a void* in a structure

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    It's ok to use them in structs.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    sizeof

    its ok to put any kind of datatype in struct

    Its seems your error is in
    Code:
    // p = malloc(sizeof(UNIVAR*));
    // should be
    p = malloc(sizeof(UNIVAR));

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    ill be damned thx m8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Compiling c++ intrinsics commands
    By h3ro in forum C++ Programming
    Replies: 37
    Last Post: 07-13-2008, 05:04 AM
  3. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  4. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 02:54 PM
  5. Getting position from game..
    By brietje698 in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2007, 12:15 PM