difference between type conversion and type casting

This is a discussion on difference between type conversion and type casting within the C Programming forums, part of the General Programming Boards category; Hi ! Can any body tell me difference between typeconversion and type casting. Plz if possible, explain with example... Thanks ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    difference between type conversion and type casting

    Hi !

    Can any body tell me difference between typeconversion and type casting.

    Plz if possible, explain with example...

    Thanks in advanced

    bargi

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,843
    A type conversion is a conversion between types. For example;
    Code:
    int main()
    {
        char x;
        int i;
        x = 'a';
        i = x;       /*   Implicitly convert x to int, and assign the result to i */
    }
    A cast is used to make the conversion explicit. So the above could be done using a cast.
    Code:
    int main()
    {
        char x;
        int i;
        x = 'a';
        i = (int)x;       /*   Explicitly convert x to int, assign the result to i */
    }
    In some cases, a conversion is technically possible but disallowed under the rules of the language. For example;
    Code:
    int main()
    {
        char ax[2];
        char *px;
        int *pi;
         
        px = ax;      /* Implicitly convert ax into a pointer to char */
    
        pi = px;       /*   Attempt to implicitly convert px from a pointer to char into a pointer to int */
        *pi = 42;
    }
    This code will yield a compiler error on the commented line because an implicit conversion between pointer types (unless one is void) is disallowed. The compiler can be bludgeoned into submission, so it allows the conversion, by using a cast.
    Code:
    int main()
    {
        char ax[2];
        char *px;
        int *pi;
    
        px = ax;      /* Implicitly convert ax into a pointer to char */
    
        pi = (int *)px;       /*   Explicitly convert px from a pointer to char into a pointer to int */
        *pi = 42;
    }
    This code will now compile, but the line "*pi = 42;" now yields undefined behaviour (among other things, that is the reason the implicit conversion is disallowed).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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