Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Amazing !!!!!!

Amazing !!!!!!

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chintan
    wrote on last edited by
    #1

    Look at following C statements: const char ch = 'c'; char* pc = (char*)&ch; *pc = 'd'; printf("%c[%u]\n%c[%u]",ch,&ch,*pc,pc); Output of this code is as follows: c[6684148] d[6684148] What I want to know is if both addresses are same then how the value can be different? Thanking You... C.R.Naik

    D R 2 Replies Last reply
    0
    • C Chintan

      Look at following C statements: const char ch = 'c'; char* pc = (char*)&ch; *pc = 'd'; printf("%c[%u]\n%c[%u]",ch,&ch,*pc,pc); Output of this code is as follows: c[6684148] d[6684148] What I want to know is if both addresses are same then how the value can be different? Thanking You... C.R.Naik

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      I suspect that when printf() was evaluating the variables, it knew that 'ch' was a const, thus could only have its original value (i.e., why bother re-evaluating something that is known not to change). Step through this code with the debugger and watch the changes each statement makes.

      1 Reply Last reply
      0
      • C Chintan

        Look at following C statements: const char ch = 'c'; char* pc = (char*)&ch; *pc = 'd'; printf("%c[%u]\n%c[%u]",ch,&ch,*pc,pc); Output of this code is as follows: c[6684148] d[6684148] What I want to know is if both addresses are same then how the value can be different? Thanking You... C.R.Naik

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        Chintan wrote: Amazing !!!!!! Not really. The compiler treats constant variables very similarly to #defines. It replaces the value of ch with 'c', which is what it is initialised with, simply because it is constant. The compiler does not care what sort of pointer trickery you perform. It assumes that a constant variable is constant, which it should be. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
        Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups