Thread: Typecasting

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    18

    Typecasting

    Code:
     struct info{    
        float one;
        int two;
        int three;
        int four;
        ...
        ...
        ...
    };
    
    int main()
    
    {
    
            struct info time, stime;
    
            time.one = 1;
            time.two = 2;
            time.three = 3;
            time.four = 4;
            time.five = 5;
            time.six = 6;
            time.seven = 7;
            time.eight = 8;
            time.nine = 9;
            time.ten = 10;
            time.eleven = 11;
            time.twelve = 12;
            time.thirteen = 13;
            time.fourteen = 14;
            time.fifteen = 15;
            time.sixteen = 16;
            time.seventeen = 17;
            time.eighteen = 18;
            time.nineteen = 19;
            time.twenty = 20;
            stime.one = 2.4 ;
    Have a struct. Every element is initialized as type int. Obviously, the 2.4 will be truncated to 2. When I initialize "one" as type float before main, and call "stime.one" later in the program, i get some type of garbage value (1072693248). I tried leaving it as int and typecasting, no luck. Can't find a solution to this, any ideas?
    Thanks in advance, let me know if more info is needed.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look at line 2. The member "one" is of type float, so the value 2.4 will not be converted to an int within the expression "stime.one = 2.4". The value 2.4 is of type double and will be converted to an float (i.e. the value 2.4f).

    I won't even ask why you're trying to have 20 distinct variables to hold 20 numeric values in sequence. Arrays are designed for such things.

    You will need to provide a SMALL but COMPLETE sample of code (i.e. one that people can compile/build/execute without modification) that demonstrates your problem. Odds are, some code is molesting a pointer, or falling off the end of an array, or is defining something differently than you've shown.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    18
    Quote Originally Posted by grumpy View Post
    Look at line 2. The member "one" is of type float, so the value 2.4 will not be converted to an int within the expression "stime.one = 2.4". The value 2.4 is of type double and will be converted to an float (i.e. the value 2.4f).

    I won't even ask why you're trying to have 20 distinct variables to hold 20 numeric values in sequence. Arrays are designed for such things.

    You will need to provide a SMALL but COMPLETE sample of code (i.e. one that people can compile/build/execute without modification) that demonstrates your problem. Odds are, some code is molesting a pointer, or falling off the end of an array, or is defining something differently than you've shown.
    I do understand that this same task could be completed using arrays. I am however, trying to cement my understanding in every aspect of C, and structures were something that needed to be contrived with to fit any use in my program at all.

    Nevertheless, I discovered that my problem was forgetting to replace the integer placeholder, %d with the floating point placeholder, %f. Very foolish mistake...
    Thank you for your input, Grumpy, it was very well appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Typecasting Help
    By 127.0.0.1 in forum C Programming
    Replies: 7
    Last Post: 03-11-2010, 02:26 AM
  2. typecasting
    By eklavya8 in forum C Programming
    Replies: 9
    Last Post: 01-03-2009, 09:02 AM
  3. typecasting
    By sreetvert83 in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2005, 01:55 PM
  4. typecasting
    By v6sa in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2005, 04:01 AM
  5. typecasting
    By JaWiB in forum C++ Programming
    Replies: 14
    Last Post: 06-01-2003, 10:42 PM