473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
Nov 14 '05 #1
14 2505
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_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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2


Je***********@physik.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_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 ___ Je***********@physik.fu-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)
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***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4


Je***********@physik.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
--
"Combination 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***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6
Je***********@physik.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.helsinki.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.helsinki.fi> wrote:
Je***********@physik.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***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #8
Je***********@physik.fu-berlin.de scribbled the following:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Je***********@physik.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.helsinki.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


Je***********@physik.fu-berlin.de wrote:

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;-)


We (in India) call experienced persons as sir.
I have to be careful with cross cultured group.
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.


Thanks for the code. I just downloaded Kazlib which has excecp.c that
deals with ANSI way of exception handling in C. I am going through that.
--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
Nov 14 '05 #11
sathya_me <sa****@nomail.com> wrote:
Thanks for the code. I just downloaded Kazlib which has excecp.c that
deals with ANSI way of exception handling in C. I am going through that.


Ok, that was what you looking for, something like C++ exceptions
for C. But be aware that no exceptions get thrown automatically
for e.g. overflows, devision by zero etc. It's just meant for cases
where you throw an exception explicitely in your code. And another
point you have to remember is that the use of setjmp/longjmp, which
get used in the kazlib, might lead to some strange effects under
some conditions: Since setjmp() isn't guaranteed to store the
contents of all CPU registers it might happen that variables that
the compiler put into processor registers (which is quit common
when you switch on optimization) may have become "clobbered" after
throwing an exception, i.e. they may have a different value from
what they had before the try block started. I would recommend
that you set your compiler to a high warning level - it might
give you some hints about such possible problems and in that
case you will have to declare the affected variables as volatile
(or use some other method that guarantees that the compiler won't
put them into CPU registers).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #12
On 10 Aug 2004 11:22:13 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
Je***********@physik.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?


If you're not fussy about *which* British queen, walk into any pub in Soho
on a friday night.... (g,d&r).

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #13
Je***********@physik.fu-berlin.de wrote:
sathya_me <sa****@nomail.com> wrote:
Thanks for the code. I just downloaded Kazlib which has excecp.c
that deals with ANSI way of exception handling in C. I am going
through that.


Ok, that was what you looking for, something like C++ exceptions
for C. But be aware that no exceptions get thrown automatically
for e.g. overflows, devision by zero etc. It's just meant for cases

.... snip ...

They may very well be, on the better hardware. Not PCs. It is a
quite allowable form of undefined behavior.

--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"A man who is right every time is not likely to do very much."
- Francis Crick, co-discover of DNA
Nov 14 '05 #14
CBFalconer <cb********@yahoo.com> wrote:
Je***********@physik.fu-berlin.de wrote:
sathya_me <sa****@nomail.com> wrote:
Thanks for the code. I just downloaded Kazlib which has excecp.c
that deals with ANSI way of exception handling in C. I am going
through that.
Ok, that was what you looking for, something like C++ exceptions
for C. But be aware that no exceptions get thrown automatically
for e.g. overflows, devision by zero etc. It's just meant for cases

... snip ...

They may very well be, on the better hardware. Not PCs. It is a
quite allowable form of undefined behavior.


I didn't made myself clear enough - I meant "exceptions won't get
thrown automatically for e.g. overflows etc." by the "kazlib" (by
Kaz Kylheku), which the OP said she downloaded - at least a quick
glance at the code didn't show me anything that would do that. Of
course, if your hardware signals such conditions I guess you could
throw that kind of exception from the handler for the condition.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #15

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

Similar topics

3
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...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
6
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: ...
6
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...
669
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...
7
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...
4
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....
12
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: ...
20
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shęllīpōpļ 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.