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. Funny C++

Funny C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++
6 Posts 4 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.
  • A Offline
    A Offline
    Almindra
    wrote on last edited by
    #1

    Who can explain : char *m="az"; cout << m << endl; i=0; cout << m[i++] << m[i++] << endl ; i=0; ((cout.operator<<(m[i++])).operator <<( m[i++])).operator <<(endl) ; Gives (Visual C++) 6.0 : az za za Thanks .

    D J 2 Replies Last reply
    0
    • A Almindra

      Who can explain : char *m="az"; cout << m << endl; i=0; cout << m[i++] << m[i++] << endl ; i=0; ((cout.operator<<(m[i++])).operator <<( m[i++])).operator <<(endl) ; Gives (Visual C++) 6.0 : az za za Thanks .

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

      Using more than one ++ or -- operator on anything but a line by themselves is always risky. Looking at the statement:

      cout << m[i++] << m[i++] << endl;

      the i++ statement gets executed first, and that value (1) is used with the [] operator.


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      A T 2 Replies Last reply
      0
      • A Almindra

        Who can explain : char *m="az"; cout << m << endl; i=0; cout << m[i++] << m[i++] << endl ; i=0; ((cout.operator<<(m[i++])).operator <<( m[i++])).operator <<(endl) ; Gives (Visual C++) 6.0 : az za za Thanks .

        J Offline
        J Offline
        jbarton
        wrote on last edited by
        #3

        In the second and third cout statements, you are modifying the value of i more than 1 time between sequence points. The result of this is undefined behavior - the compiler can output anything it wants.

        1 Reply Last reply
        0
        • D David Crow

          Using more than one ++ or -- operator on anything but a line by themselves is always risky. Looking at the statement:

          cout << m[i++] << m[i++] << endl;

          the i++ statement gets executed first, and that value (1) is used with the [] operator.


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          A Offline
          A Offline
          Almindra
          wrote on last edited by
          #4

          sorry but the value of i used by the [] operator will be 0 ... I think you're confused whith ++i ...

          D 1 Reply Last reply
          0
          • D David Crow

            Using more than one ++ or -- operator on anything but a line by themselves is always risky. Looking at the statement:

            cout << m[i++] << m[i++] << endl;

            the i++ statement gets executed first, and that value (1) is used with the [] operator.


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            Um, no. The problem here is the undefined behavior of the multiple ++ operations which does get you into some problems. int i = 0; cout << m[i++] << endl; This code would fetch the 0'th value. The ++ and -- operators are not risky when you know how to use them. They have well defined behaviors and undefined behaviors. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            1 Reply Last reply
            0
            • A Almindra

              sorry but the value of i used by the [] operator will be 0 ... I think you're confused whith ++i ...

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

              cout << i++ << i++ << endl;

              Stepping into the code for ostream& ostream::operator<<(int n) revealed that n had a value of 1 the first time, and a value of 0 the second time. Had the pre-increment operator been used instead, a subscript-out-of-range error would have been realized since the value of n would have been 2 the first time through. Separating the i++ operations into two statements solves all the problems.


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              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