Typecasting -- Good or Bad

This is a discussion on Typecasting -- Good or Bad within the C Programming forums, part of the General Programming Boards category; Hello All, I have been writing C programs since 5 years. Just wondering, what happens when we typecast any variable.? ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Typecasting -- Good or Bad

    Hello All,

    I have been writing C programs since 5 years. Just wondering, what happens when we typecast any variable.?

    I would appreciate your comments.

    Thanks,

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,248
    Typecasting is just an instruction to the compiler to;

    1) not reject (i.e. fail to compile) an obviously invalid type conversion;
    2) not complain (i.e. give a warning or error message) about a potentially invalid type conversion.

    In both cases typecasting is a means for the programmer to tell the compiler "don't complain, I know what I'm doing". The most common causes of problems associated with typecasting come in when the programmer really doesn't know what he or she is doing (eg casting of pointer types between incompatible types) but uses a typecast to stop the compiler complaining about an invalid operation.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    A type cast also may cause the compiler to either do a conversion from the type of the variable to another type, where that conversion may be non-trivial, as in
    Code:
    int intval = 2;
    double dblval = sin((double)intval);
    Another legitimate place for a type cast is when converting a void * to a known type or vica-versa, as in
    Code:
    struct alpha
    {
        struct alpha *a_next;
        struct alpha *a_prev;
        int a_type;
        double a_value;
        char a_name[40];
    };
    
    struct alpha *element = (struct alpha *)malloc(sizeof(struct alpha));
    Insert obnoxious but pithy remark here

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >Another legitimate place for a type cast is when converting a void * to a known type or vica-versa
    Only if you're using a REALLY old compiler, or C++. C has allowed implicit conversions to and from void* for a long time now, so there's really no point in using a cast in those cases. But that's my opinion. I know a bunch of people that still use the cast as a matter of style because they think it's more informative and like to type.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,248
    Quote Originally Posted by filker0
    A type cast also may cause the compiler to either do a conversion from the type of the variable to another type, where that conversion may be non-trivial, as in
    Code:
    int intval = 2;
    double dblval = sin((double)intval);
    Interestingly enough, in practice, when I find myself in a position of trying to pass an int to a math function that expects a double, a check through preceding code usually identifies a variable that is declared as int, but should really be a double.

    In any event, the following achieves the same as the above, with less typing.
    Code:
    int intval = 2;
    double dblval = sin(intval*1.0);
    While, literally, this does something more (it converts intval to double as required, then multiplies it by 1.0, and passes the result) very few compilers fail to optimise "intval*1.0" and treat it only as a conversion. And, if some "smart" maintainer comes along an removes the multiplication on the basis that it "doesn't do anything" their reward is a complaining compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Oh this one is GOOD!
    By sean in forum A Brief History of Cprogramming.com
    Replies: 74
    Last Post: 11-11-2008, 01:30 AM
  2. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  4. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21