Thread: determine type of void

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    determine type of void

    Hi All,

    I have a void* that is a pointer to either a char* or an int.

    How do I determine which type the pointer is pointing to?

    Thanks for your help,

    rotis23

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't
    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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't directly from the pointer. You have to use some form of a trick.

    Usually, a second parameter is passed to a function receiving type void * that specifies the type. Something like:
    Code:
    void func(void *ptr, int type)
    {
      if(type == 1)
      {
        // do char * stuff
      }
      else
      {
        // do int stuff
      }
    }
    Another option would be to embed the type somewhere in the pointer like at the head of it.
    Code:
    void func(void *ptr)
    {
      int type = *(int *)ptr;
    
      ((int *)ptr)++;
    
      if(type == 1)
      // etc., same as code above.
    }
    Last edited by itsme86; 08-25-2004 at 11:50 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can use a structure/union to pass around multiple types neatly:
    Code:
    enum var_type { TYPE_INT, TYPE_CHARPTR };
    
    struct variant_t
    {
        enum var_type;
    
        union 
        {
             int int_val;
             char* charptr_val;
        } var_value;
    };
    For a mega sample see the Windows VARIANT type.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's an example of usage:
    Code:
    itsme@dreams:~/C$ cat void.c
    #include <stdio.h>
    
    enum { STR_TYPE, INT_TYPE };
    
    void func(void *ptr, int type)
    {
      switch(type)
      {
        case STR_TYPE:
          printf("It's a str! The str is: %s\n", (char *)ptr);
          break;
        case INT_TYPE:
          printf("It's an int! The int is: %d\n", *(int *)ptr);
          break;
        default:
          printf("It's an unknown type!\n");
          break;
      }
    }
    
    int main(void)
    {
      char str[] = "Hello, world!";
      int num = 86;
    
      func(str, STR_TYPE);
      func(&num, INT_TYPE);
    
      return 0;
    }
    And when I run it...
    itsme@dreams:~/C$ ./void
    It's a str! The str is: Hello, world!
    It's an int! The int is: 86
    itsme@dreams:~/C$
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks people!

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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 04:17 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM