473,545 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking the type of a variable with equality operator

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

--
"Combinatio n is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
Nov 14 '05 #1
14 2525
sathya_me <sa****@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_wi th_type_informa tion {
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 ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2


Je***********@p hysik.fu-berlin.de wrote:

sathya_me <sa****@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.


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_wi th_type_informa tion {
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 ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de


--
"Combinatio n is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
Nov 14 '05 #3
sathya_me <sa****@nomail. com> 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 ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4


Je***********@p hysik.fu-berlin.de wrote:

sathya_me <sa****@nomail. com> 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
--
"Combinatio n is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
Nov 14 '05 #5
sathya_me <sa****@nomail. com> 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 ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #6
Je***********@p hysik.fu-berlin.de scribbled the following:
sathya_me <sa****@nomail. com> 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 (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Show me a good mouser and I'll show you a cat with bad breath."
- Garfield
Nov 14 '05 #7
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Je***********@p hysik.fu-berlin.de scribbled the following:
sathya_me <sa****@nomail. com> 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 ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #8
Je***********@p hysik.fu-berlin.de scribbled the following:
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Je***********@p hysik.fu-berlin.de scribbled the following:
sathya_me <sa****@nomail. com> 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 (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 14 '05 #9
> 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"

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2105
by: Vinodh Kumar P | last post by:
Whenever I read any C++ literature I find the words "C++ is statically type checked".OK.Agreed. Is there any language that supports "Dynamic type checking"? In such a case any type can be assigned to any other type during compile time?
99
5049
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
6
4487
by: Edward Diener | last post by:
Now that operator overloading allows to ref classes to be compared for equality using == syntax, how does one compare the actual ref pointers ( ^ ) for equality instead ? As an example: SomeRefObject ^ obj1(..initialized somehow); SomeRefObject ^ obj2(..initialized somehow); if (obj1 == obj2) // This compares the objects themselves for...
6
319
by: KOFKS | last post by:
I have come across a problem when I'm reading "comp.lang.c FAQ list Question 12.1". This item is about char c; while((c = getchar()) != EOF) ... One paragraph of the item writes:"If type char is unsigned, an actual EOF value will be truncated (by having its higher-order bits discarded, probably resulting in 255 or 0xff) and will not be...
669
25537
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
7
2845
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
4
11663
by: chinu | last post by:
HI all, i am declaring an array in javascript var a = new array(); now before assigning a value to the ith element of this array, i have to check if some value has already been assigned there. But at the time of defining array, it gives undefined values to the array. Is there a way/function, thru which i can find, whether a particular...
12
2200
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ------------------------------- PROGRAMME -------------------------- /* Section 2.7 type conversions we are asked to write a function "htoi(an array)" that...
20
3086
by: nicolas.pourcelot | last post by:
Hi, I want to test if an object IS in a list (identity and not equality test). I can if course write something like this : test = False myobject = MyCustomClass(*args, **kw) for element in mylist: if element is myobject:
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.