Thread: why does this work?

  1. #1
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86

    why does this work?

    I saw something like this but I can't remember where
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        const unsigned int var = 0xabcd1234;
    
        printf("%x\n", var);
        ((unsigned char *)&var)[0] = 0xff;
        printf("%x\n", var);
    
        return(0);
    }
    it was successfully compiled using gcc with "-Wall", "-W", and "-pedantic" options.
    why does it work? I don't understand.
    is it one of what they say about C that "you can shoot your own foot" or something like that?

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    no, you are just recasting a pointer to a different type then manipulating that type. Sure you can shoot yourself fin the foot if you dont know what you are doing though. And it only works if you know what to expect as far as the endianness of the machine.

  3. #3
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    thank you very much

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cph View Post
    I saw something like this but I can't remember where
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        const unsigned int var = 0xabcd1234;
    
        printf("%x\n", var);
        ((unsigned char *)&var)[0] = 0xff;
        printf("%x\n", var);
    
        return(0);
    }
    it was successfully compiled using gcc with "-Wall", "-W", and "-pedantic" options.
    why does it work? I don't understand.
    is it one of what they say about C that "you can shoot your own foot" or something like that?
    What do you mean by "work"? Anyone saying whether something works or not is only telling us whether it did what they expected or not. It doesn't tell us what they expected it to do to begin with.
    So, what did you expect it to do that it actually did do, and why was it surprising?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He means he's changing the value of something declared const. You can do pretty much the same thing in C++. I suppose it's only noteworthy because:

    a) it's a "trick".
    b) it's a "bug" that could be a huge pain in the ass to track down.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by quzah
    You can do pretty much the same thing in C++.
    Yes, though if you only use C++ style casts, then you need both a const_cast and a reinterpret_cast, either of which should raise alarm bells to a reader.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh, I didn't even notice the const there!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    That is cruel
    Last edited by slingerland3g; 11-24-2009 at 02:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM