Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Checking the type of a variable with equality operator

Reply
Thread Tools

Checking the type of a variable with equality operator

 
 
sathya_me
Guest
Posts: n/a
 
      08-08-2004
Dear clc,
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

I thought of using sizeof(type). But it varies according to the
system.
<limits.h> #define values are defined to make a range for a variable
but
it does not checks the type. Those macro constants are used only
after we
declare a variable's type.

How can I do this?
Am I missing something basic?
Please explain.
Thanks

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-08-2004
sathya_me <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
sathya_me
Guest
Posts: n/a
 
      08-08-2004


(E-Mail Removed)-berlin.de wrote:
>
> sathya_me <(E-Mail Removed)> 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.
>


Thanks for the information which I have read in my C reference. But
my mind
got stilled when I thought about the idea of using the == to
determine the type
of a variable.


> > 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.


Thanks again
>
> 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


To be more precise:
After going through some of the old thread of clc under the topic
"overflow / underflow"
I thought of writing a function which could tackle all possible
overflow / underflow
with multiplication, division , divide by zero , giving more
(less)value to the input,
etc.(just a try from a beginner).
So the function declaration goes like this
void overflow_action (void *type , unsigned action)
if(type == int_type)
check *limit* for the variable by using #define constants from
<limits.h>;
action for overflow;
if(type == float_type)
check for limit
action for overflow;
All the same for other types too.
But I know it is different to deal with
multiplication (ie a = b * c where a > limit),
divide by zero error etc.

*I come to know that the above idea can not be worked (thanks to
Jens)*

Is there any pointer to the above (dealing with exception in
standard C)
in the net where people has done some works.




> --
> \ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
> \__________________________ http://www.toerring.de


--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-10-2004
sathya_me <(E-Mail Removed)> wrote:
> To be more precise:
> After going through some of the old thread of clc under the topic
> "overflow / underflow"
> I thought of writing a function which could tackle all possible
> overflow / underflow
> with multiplication, division , divide by zero , giving more
> (less)value to the input,
> etc.(just a try from a beginner).
> So the function declaration goes like this
> void overflow_action (void *type , unsigned action)
> if(type == int_type)
> check *limit* for the variable by using #define constants from
> <limits.h>;
> action for overflow;
> if(type == float_type)
> check for limit
> action for overflow;
> All the same for other types too.
> But I know it is different to deal with
> multiplication (ie a = b * c where a > limit),
> divide by zero error etc.


In what kind of situation would this function get called and how?
It looks a bit as if you're proposing some kind of "signaling"
mechanism for overflows and your function would be the "signal"
handler, but then the question is how the function would get
invoked at all (there's nothing that would emit such a "signal"
or would invoke suca function under these conditions - at least
nothing portable I am aware of). Or do you plan to write a "safe"
replacement for all arithmetic operations (and perhaps all math
functions) that would check the arguments before the operation
is actually done - but then the functions signature doesn't look
as if that could be done with it. Sorry, I am confused about your
intentions, could you try to explain it a bit less concise? An
example can sometimes be rather helpful

> Is there any pointer to the above (dealing with exception in
> standard C)


That would hint at your looking for some "signaling" mechanism
for overflows etc. But I fear that there's no such "exception"
mechanism (especially not in the C++ sense) to start with - unless
you mean having errno set to EDOM or ERANGE for mathematical
functions under certain circumstances (but even if this is happens
is implementation-defined as far as I can see) and maybe on plat-
forms that support it having a a SIGFPE signal raised on certain
conditions for floating point operations (i.e. IEEE 754 exceptions).

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
sathya_me
Guest
Posts: n/a
 
      08-10-2004


(E-Mail Removed)-berlin.de wrote:
>
> sathya_me <(E-Mail Removed)> wrote:
> > To be more precise:
> > After going through some of the old thread of clc under the topic
> > "overflow / underflow"
> > I thought of writing a function which could tackle all possible
> > overflow / underflow
> > with multiplication, division , divide by zero , giving more
> > (less)value to the input,
> > etc.(just a try from a beginner).
> > So the function declaration goes like this
> > void overflow_action (void *type , unsigned action)
> > if(type == int_type)
> > check *limit* for the variable by using #define constants from
> > <limits.h>;
> > action for overflow;
> > if(type == float_type)
> > check for limit
> > action for overflow;
> > All the same for other types too.
> > But I know it is different to deal with
> > multiplication (ie a = b * c where a > limit),
> > divide by zero error etc.

>
> In what kind of situation would this function get called and how?
> It looks a bit as if you're proposing some kind of "signaling"
> mechanism for overflows and your function would be the "signal"
> handler, but then the question is how the function would get
> invoked at all (there's nothing that would emit such a "signal"
> or would invoke suca function under these conditions - at least
> nothing portable I am aware of).


[Helping explanation sniped]

Sir,going too depth to understand for a beginner. I am just to
define a function
that will explain all the possible use of macros in <limits.h>. Just
a program
for practicing. Any links? Thanks


--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-10-2004
sathya_me <(E-Mail Removed)> wrote:
> Sir,going too depth to understand for a beginner. I am just to


No reason to "Sir" me - I've never met the British queen (and don't
expect to

> define a function
> that will explain all the possible use of macros in <limits.h>. Just
> a program
> for practicing. Any links? Thanks


Sorry, now you rather lost me completely. The only thing I can think of
at the moment is something like the following program:

---------------8<------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

enum Types {
SCHAR,
UCHAR,
CHAR,
SHRT,
USHRT,
INT,
UINT,
LONG,
ULONG
};

void print_limits( enum Types type )
{
if ( type == SCHAR )
printf( "Range of 'signed char': %d - %d (using %d bits)\n",
SCHAR_MIN, SCHAR_MAX, CHAR_BIT );
else if ( type == UCHAR )
printf( "Range of 'unsigned char': 0 - %d (using %d bits)\n",
UCHAR_MAX, CHAR_BIT );
else if ( type == CHAR )
printf( "Range of 'char': %d - %d (using %d bits)\n",
CHAR_MIN, CHAR_MAX, CHAR_BIT );
else if ( type == SHRT )
printf( "Range of 'short int': %d - %d\n", SHRT_MIN, SHRT_MAX );
else if ( type == USHRT )
printf( "Range of 'unsigned short int': 0 - %d\n", USHRT_MAX );
else if ( type == INT )
printf( "Range of 'int': %d - %d\n", INT_MIN, INT_MAX );
else if ( type == UINT )
printf( "Range of 'unsigned int': 0 - %u\n", UINT_MAX );
else if ( type == LONG )
printf( "Range of 'long int': %ld - %ld\n", LONG_MIN, LONG_MAX );
else if ( type == ULONG )
printf( "Range of 'unsigned long int': 0 - %lu\n", ULONG_MAX );
else
fprintf( stderr, "Unknown type passed to function\n" );
}

int main( void )
{
enum Types i;

for ( i = SCHAR; i <= ULONG; i++ )
print_limits( i );

return EXIT_SUCCESS;
}

---------------8<------------------------------------------------

But I don't know if that resembles anything you're interested in.

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      08-10-2004
(E-Mail Removed)-berlin.de scribbled the following:
> sathya_me <(E-Mail Removed)> wrote:
>> Sir,going too depth to understand for a beginner. I am just to


> No reason to "Sir" me - I've never met the British queen (and don't
> expect to


*I* expect to. How exactly is this done?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Show me a good mouser and I'll show you a cat with bad breath."
- Garfield
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-10-2004
Joona I Palaste <(E-Mail Removed)> wrote:
> (E-Mail Removed)-berlin.de scribbled the following:
>> sathya_me <(E-Mail Removed)> wrote:
>>> Sir,going too depth to understand for a beginner. I am just to


>> No reason to "Sir" me - I've never met the British queen (and don't
>> expect to


> *I* expect to. How exactly is this done?


I don't really know. Maybe you just call her? The only thing I am rather
sure about is that you are not supposed to call her "Lizzy" the first
time you meet her (and be very afraid of the Corgis

My best wishes to the Finish king, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      08-10-2004
(E-Mail Removed)-berlin.de scribbled the following:
> Joona I Palaste <(E-Mail Removed)> wrote:
>> (E-Mail Removed)-berlin.de scribbled the following:
>>> sathya_me <(E-Mail Removed)> wrote:
>>>> Sir,going too depth to understand for a beginner. I am just to


>>> No reason to "Sir" me - I've never met the British queen (and don't
>>> expect to


>> *I* expect to. How exactly is this done?


> I don't really know. Maybe you just call her? The only thing I am rather
> sure about is that you are not supposed to call her "Lizzy" the first
> time you meet her (and be very afraid of the Corgis


Would you happen to know her telephone number? =)

> My best wishes to the Finish king, Jens


There *is* no Finnish king. We're a republic, and our current president
is Tarja Halonen, so she can't exactly be called a "king" of any
country.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-10-2004
> void print_limits( enum Types type )
> {
> if ( type == SCHAR )
> printf( "Range of 'signed char': %d - %d (using %d bits)\n",
> SCHAR_MIN, SCHAR_MAX, CHAR_BIT );
> else if ( type == UCHAR )


What about a switch-case ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::operator<<(basic_ostream&, type) vs. std::basic_ostream::operator(type)... ? Martin T. C++ 7 03-10-2008 10:42 AM
equality operator based on position toton C++ 3 03-02-2007 01:15 PM
Problem about type promotion and equality operator. KOFKS C Programming 6 06-07-2006 06:21 PM
Problem about type promotion and equality operator. KOFKS C Programming 2 06-06-2006 10:32 AM
Equality operator italy Python 5 03-05-2005 10:18 PM



Advertisments