Thread: pointer conversion problem

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    pointer conversion problem

    1)
    Code:
    float f = 65.00f;
    int * ip = (int*)&f;
    printf("%i", *ip);
    prints garbage value instead. Why?
    When i try to convert it double*, it prints 0.000000. I guess that it is because of the memory size difference of 4 bytes of float and 8 bytes of double.

    When i try the similar between int* and char*, it converts fine, is it because of fact that C stores character as decimal equivalent?

    2)
    Code:
    int * ip = (int[]){1,2,3,4};
    ip[2] = 5;
    workes fine. Why? Isn't it supposed to crash the program or cause so-called "undefined behaviour"?

    Thank you

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    prints garbage value instead. Why?
    You've told the compiler and `printf' that the memory at location `*ip' and parameter is an `int'.

    You are wrong.

    The memory at location `&f' is a `float'.

    When i try the similar between int* and char*, it converts fine, is it because of fact that C stores character as decimal equivalent?
    I'm sure it compiles fine. C and C++ compilers are happy to let you pretend almost anything you want to pretend.

    I'm equally sure that it doesn't actually work as you think.

    workes fine. Why?
    This isn't doing what you think.

    This uses a bit of C that anonymously creates a placeholder array (The standard has specific wording for this construct, and it isn't "anonymous placeholder", but I can't be bothered to check.) and assigns the location of that placeholder to a pointer.

    Isn't it supposed to crash the program or cause so-called "undefined behaviour"?
    This isn't undefined behavior where the compiler accepts this construct.

    With that said, undefined behavior can result in almost anything.

    It doesn't have to crash. The behavior is undefined. It is entirely possible it works in `main' but fails in a different function.

    Soma

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    To clarify the above post:

    The in-memory representation of a float bears no resemblance to the in-memory representation of an int.

    So when you take a float, tell the compiler to read it like an int NO MATTER WHAT (that's what casts do and rarely give errors because if you're casting you should KNOW what you're doing and the computer CANNOT know what you're doing), and then print the result you end up with gobbledegook.

    It's like giving someone a word in French and telling them it should be pronounced in English NO MATTER WHAT and then wondering what the hell "silver plate" means and why it means "please".

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    Thank you, i really appreciate it. So, it seems that in the second case, it would be equivalent to creating an array and making the pointer point to it?

    I have already read many C Books back and forth. But, Which book should i read to learn things in such details?

    Thank you

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    A classic book is the one of KERNIGHAN & RICHIE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generic pointer to char conversion problem
    By monkey_c_monkey in forum C++ Programming
    Replies: 5
    Last Post: 07-12-2012, 07:06 AM
  2. Nonportable pointer conversion - please help
    By amir1986 in forum C Programming
    Replies: 2
    Last Post: 01-20-2011, 04:21 AM
  3. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  4. invalid pointer conversion
    By jimzy in forum C++ Programming
    Replies: 8
    Last Post: 04-13-2007, 12:21 PM
  5. help me in conversion using pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-20-2002, 12:15 AM