Re: Checking the type of a variable with equality operator

Jens.Toerring_at_physik.fu-berlin.de
Date: 08/08/04


Date: 8 Aug 2004 12:30:52 GMT

sathya_me <sathya@nomail.com> wrote:
> Dear clc,
u> I have a variable void *a; Since variable "a" can be assigned (point
> to) any type and also
> any type can be assigned to "a" (i.e means "a" = any typed variable;
> any typed variable = "a".
> Considering the above I have a function, which is declared and defined
> to take any type
> of parameter with void*
> return-type foo (void *a);
> In the processes of assignment of value to the variable "a" I want to
> check the *type*
> of variable "a" at each of assignment with the equality operator.
> Like
> if(a == INT_TYPE)
> some action
> if(a == FLOAT_TYPE)
> some action

> How can I do this?

The answer is simple - you can't. When you pass a void pointer to
the function you have _no_ information about the type of what the
pointer you have cast to void is pointing to (it doesn't even have
to point to anything that may have had a type - if it's a pointer
you got from malloc() it's just a pointer to some memory that can
be used to store data of arbitrary type). That's also why you can't
dereference a void pointer or do pointer arithmetic with void
pointers - both that would require type information.

> Am I missing something basic?

The basic problem is that type information is a compile time only
concept. Once the program has been compiled there's nothing left
of this information, a pointer is just a variable that can hold an
address with no type information attached to it. The type informa-
tion has been used by the compiler to pick the correct machine
instructions when the value of what the pointer points to is used,
to do type checking and to determine by how many bytes a pointer
has to be changed when it e.g. gets incremented.

If you need type information within the program at run time all you
can do is to associate another variable with the pointer that tells
you what type it is supposed to point to, e.g. you could have a
structure

struct void_pointer_with_type_information {
    void *a;
    int type_of_a;
};

where 'type_of_a' gets set to a unique value describing the type
of the variable 'a' is pointing to and then work with that instead
of 'naked' void pointers.
                                    Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... cast to unsigned long and cast back in the callback. ... number - not a pointer cast to unsigned long, not an index in array, etc. ...
    (Linux-Kernel)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... You can't do pointer arithmetic on a void* value. ... qsort behaves in a manner consistent with its specification. ... actually works as advertised...but doesn't mean that the cast is ...
    (comp.lang.c)
  • Re: The void** pointer breaking symmetry?
    ... void** is a generic pointer type that can be implicitly converted ... because dereferencing the void ** variable once gives ...
    (comp.lang.c)
  • Re: Maps, filters and accumulators
    ... void pointer they get back to a pointer to the appropriate type of data. ... int(*compar)(const void *, const void *)) ... avoid having to do all that work (either by getting a code generator to ...
    (comp.lang.c)