Re: Checking the type of a variable with equality operator
Jens.Toerring_at_physik.fu-berlin.de
Date: 08/08/04
- Next message: pete: "Re: Are these behaviors defined?"
- Previous message: pete: "Re: C objects"
- In reply to: sathya_me: "Checking the type of a variable with equality operator"
- Next in thread: sathya_me: "Re: Checking the type of a variable with equality operator"
- Reply: sathya_me: "Re: Checking the type of a variable with equality operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: pete: "Re: Are these behaviors defined?"
- Previous message: pete: "Re: C objects"
- In reply to: sathya_me: "Checking the type of a variable with equality operator"
- Next in thread: sathya_me: "Re: Checking the type of a variable with equality operator"
- Reply: sathya_me: "Re: Checking the type of a variable with equality operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|