Thread: Of pointer typecasting and other matters...

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    Of pointer typecasting and other matters...

    Dear Salem,

    At risk of abusing your generosity, I'm going to pester you with another question. Well actually, two.

    The first one is a request for a good C book recommendation. Something not geared toward beginners, and that does not shy away from explaining the low level stuff and details of what is going on behind the scenes.
    I own K&R's The C Programming language and although I admit I haven't read it from cover to cover but rather use it as a reference manual, consulting it when I want to learn about a topic, I find it's depth lacking in several subjects. One example being the topic of my other thread, variadic arguments. I had to learn about this by using the internet.

    The other question relates to typecasting. This is one of those subjects I'd like to study in detail to really understand what is going on at a deeper level. Specifically pointer typecasting.
    I can do this just fine:
    Code:
    void *vptr;
    
    vptr = (int*)45;
    Yet In other circumstances, I get a warning about a cast to pointer from integer of different size, which I "solve" by casting to long. Why not here? What is different in this particular case?
    More importantly, why the above works and yet this doesn't:
    Code:
    
    
    Code:
    void *vptr;
    
    vptr = (float*)4.5f; // error: cannot convert to a pointer type
    
    Finally this is OK:
    Code:
    void *ptr;
    int    *iptr;
    float *fptr;
    
    *iptr = 45;
    *fptr = 4.5f;
    vptr = iptr;
    vptr = fptr;
    Last edited by Dren; 08-16-2018 at 10:04 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    K&R is an excellent reference manual for the language itself. I trust you have a copy with a big red "ANSI-C" rubber stamp logo across it.

    But K&R only goes as far as C90, there is also C99 and C11 to contend with (a new standard appears about once a decade).
    C11 (C standard revision) - Wikipedia

    N869 is the final draft of the ISO-C99 standard (being a draft version, it's available for $0). It's great for finding out what is really guaranteed by the language and not just what your current compiler will let you get away with. If you want to write code that has the best chance of compiling on any compiler (and doing what you want), it's all good to know stuff.

    As for another book to read, then perhaps C Unleashed.

    comp.lang.c Frequently Asked Questions is also worth a read through.

    > I own K&R's The C Programming language and although I admit I haven't read it from cover to cover
    My favourite exercise then is for you to read all the references to recursion then



    > I can do this just fine:
    > vptr = (int*)45;
    Because 45 is a literal constant which the compiler will automatically widen and convert to a pointer.

    Whereas vptr = (int*)f; involves converting one stored type into another, and gives the compiler more opportunity to gripe about things which seem wrong.

    > vptr = (float*)4.5f; // error: cannot convert to a pointer type
    Well no, it wouldn't. You can't have fractional addresses in memory.

    > Finally this is OK:
    The round trip assignment of void* = type*, then at some later time type* = void* is guaranteed by the standard.

    What will cause you grief at some point is
    iptr = fptr;
    and then expecting to be able to do something meaningful with iptr.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    92
    void *ptr;
    int *iptr;
    float *fptr;

    *iptr = 45;
    *fptr = 4.5f;
    Your code above will cause runtime problems (segmentation fault) because the pointers iptr and fptr probably do not point to allocated memory
    so you cant assign a value to be contained by that address
    Those pointers are un-initialized and thus point to some random memory address; an address in un-allocated memory or maybe an address
    used by other variables (causing un-predictable results).

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    I trust you have a copy with a big red "ANSI-C" rubber stamp logo across it.
    I'm seriously spooked right now, I wasn't aware your divining skills were so developed lol.
    I only regret I will probably be forever deprived of your supernatural abilities; with all the resources you've given me, I fear I won't have to return here to ask questions ever again.
    The faq you linked alone would probably rob this website from visitors if they dared link it as a sticky. What I'm I saying, that probably wouldn't happen. People never bother to read stickies.


    On another note, I am having an emotional breakdown brought on by my inability to decide whether to buy a hard copy of C Unleashed or an ebook version I found here C Unleashed (ebook) by Richard Heathfield | 9780768657074
    As much as I like the feeling of real paper in my hands, I can't bring myself to haul the 1700 lb monstrosity that is this book -I'm loving that fact btw.
    On the other hand, the ebook is well, untangible, plus it comes with crappy DRM; I do have ways to get rid of that with Calibre though.


    So thanks to you, these are my problems right now. :cry:
    No, seriously, I can't thank you enough. Oh, and I better snatch a copy of the standard before they fisnish the thing and start charging for it. How dare they!


    Much love mein freund.
    :thumbsup:

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    104
    Quote Originally Posted by ddutch View Post
    Your code above will cause runtime problems (segmentation fault) because the pointers iptr and fptr probably do not point to allocated memory
    so you cant assign a value to be contained by that address
    Those pointers are un-initialized and thus point to some random memory address; an address in un-allocated memory or maybe an address
    used by other variables (causing un-predictable results).
    I wrote that as a quick example to illustrate a point. Thanks for pointing it out though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typecasting a char pointer
    By tubby123 in forum C Programming
    Replies: 5
    Last Post: 07-12-2011, 02:02 AM
  2. Pointer and typecasting question
    By A34Chris in forum C Programming
    Replies: 4
    Last Post: 04-28-2011, 08:14 AM
  3. Pointer Arithmetic and Typecasting
    By pobri19 in forum C Programming
    Replies: 2
    Last Post: 03-19-2009, 11:06 PM
  4. Replies: 11
    Last Post: 08-28-2008, 04:10 AM
  5. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 05:43 PM

Tags for this Thread