Checking the type of a variable with equality operator

Discussion in 'C Programming' started by sathya_me, Aug 8, 2004.

  1. sathya_me

    sathya_me Guest

    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)
     
    sathya_me, Aug 8, 2004
    #1
    1. Advertising

  2. sathya_me

    -berlin.de Guest

    sathya_me <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 8, 2004
    #2
    1. Advertising

  3. sathya_me

    sathya_me Guest

    -berlin.de wrote:
    >
    > sathya_me <> 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 ___ -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)
     
    sathya_me, Aug 8, 2004
    #3
  4. sathya_me

    -berlin.de Guest

    sathya_me <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 10, 2004
    #4
  5. sathya_me

    sathya_me Guest

    -berlin.de wrote:
    >
    > sathya_me <> 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)
     
    sathya_me, Aug 10, 2004
    #5
  6. sathya_me

    -berlin.de Guest

    sathya_me <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 10, 2004
    #6
  7. [OT] Checking the type of a variable with equality operator

    -berlin.de scribbled the following:
    > sathya_me <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Show me a good mouser and I'll show you a cat with bad breath."
    - Garfield
     
    Joona I Palaste, Aug 10, 2004
    #7
  8. sathya_me

    -berlin.de Guest

    Re: [OT] Checking the type of a variable with equality operator

    Joona I Palaste <> wrote:
    > -berlin.de scribbled the following:
    >> sathya_me <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 10, 2004
    #8
  9. Re: [OT] Checking the type of a variable with equality operator

    -berlin.de scribbled the following:
    > Joona I Palaste <> wrote:
    >> -berlin.de scribbled the following:
    >>> sathya_me <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To err is human. To really louse things up takes a computer."
    - Anon
     
    Joona I Palaste, Aug 10, 2004
    #9
  10. > 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"
     
    Emmanuel Delahaye, Aug 10, 2004
    #10
  11. sathya_me

    sathya_me Guest

    -berlin.de wrote:
    >
    > sathya_me <> 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)
     
    sathya_me, Aug 10, 2004
    #11
  12. sathya_me

    -berlin.de Guest

    sathya_me <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 10, 2004
    #12
  13. Re: [OT] Checking the type of a variable with equality operator

    On 10 Aug 2004 11:22:13 GMT, in comp.lang.c , Joona I Palaste
    <> wrote:

    >-berlin.de scribbled the following:
    >> sathya_me <> 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 =---
     
    Mark McIntyre, Aug 10, 2004
    #13
  14. sathya_me

    CBFalconer Guest

    -berlin.de wrote:
    > sathya_me <> 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
     
    CBFalconer, Aug 11, 2004
    #14
  15. sathya_me

    -berlin.de Guest

    CBFalconer <> wrote:
    > -berlin.de wrote:
    >> sathya_me <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Aug 11, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. italy

    Equality operator

    italy, Mar 5, 2005, in forum: Python
    Replies:
    5
    Views:
    7,026
    Marc 'BlackJack' Rintsch
    Mar 5, 2005
  2. KOFKS
    Replies:
    2
    Views:
    286
    KOFKS
    Jun 6, 2006
  3. KOFKS
    Replies:
    6
    Views:
    295
    Keith Thompson
    Jun 7, 2006
  4. toton
    Replies:
    3
    Views:
    338
    toton
    Mar 2, 2007
  5. Martin T.
    Replies:
    7
    Views:
    840
    Martin T.
    Mar 10, 2008
Loading...

Share This Page