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. The Lounge
  3. Learnt something "new" about C/C++

Learnt something "new" about C/C++

Scheduled Pinned Locked Moved The Lounge
questionc++comhostingcloud
36 Posts 17 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.
  • S Super Lloyd

    I guess it's only for type of the same size as int.... and a good source of endless bug and fun! :-\

    All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

    A Offline
    A Offline
    Afzaal Ahmad Zeeshan
    wrote on last edited by
    #7

    Nah, on that link, it was shared that compiler runs a multiplication with sizeof(a) itself. :-)

    The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

    1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #8

      Nah! It's for C++ people. Those people love those kind of shoot in your own foot kind of tricks! Show the world that only real man (and real woman too, of course) dare to C++! :laugh: :rolleyes:

      All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

      F 1 Reply Last reply
      0
      • A Afzaal Ahmad Zeeshan

        I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is a[5] == 5[a]?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:

        int main()
        {
        int a[5] = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; i++) {
        std::cout << i[a] << ", ";
        }
        std::cout << std::endl;
        system("pause");
        return 0;
        }
        // Output: 1, 2, 3, 4, 5,

        For those who didn't know it (just like me!) the logic is that C (or C++) translates a[5] to *(a + 5) and then gets the data from that location. Which, is similar to having 5[a] that gets translated to *(5 + a). Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #9

        Afzaal Ahmad Zeeshan wrote:

        I am going to have an exam of C++ tomorrow

        Good luck on the exam!

        Afzaal Ahmad Zeeshan wrote:

        go back to Haskell once again

        Yikes! :omg:

        Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

        Regards, Sander

        A 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          T Offline
          T Offline
          Tim Carmichael
          wrote on last edited by
          #10

          Agreed.. because you can doesn't mean you should.

          1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            Afzaal Ahmad Zeeshan wrote:

            I am going to have an exam of C++ tomorrow

            Good luck on the exam!

            Afzaal Ahmad Zeeshan wrote:

            go back to Haskell once again

            Yikes! :omg:

            Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

            Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

            Regards, Sander

            A Offline
            A Offline
            Afzaal Ahmad Zeeshan
            wrote on last edited by
            #11

            Quote:

            Afzaal Ahmad Zeeshan wrote:

            go back to Haskell once again

            Yikes! :OMG:

            Trust me, Haskell is much easy and simple language. Although I started learning Haskell 2 days ago, I think I am enjoying the much simpler and better syntax. After all, you also like PHP[^]. ;-p Don't you Sander? :laugh:

            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

            Sander RosselS 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              Mike HankeyM Offline
              Mike HankeyM Offline
              Mike Hankey
              wrote on last edited by
              #12

              But we both know once something is seen it cannot be unseen and that it will be done just to amaze and confound others.

              New version: WinHeist Version
              You didn't fall from the stupid tree you got dragged through the whole dumbass forest.

              1 Reply Last reply
              0
              • A Afzaal Ahmad Zeeshan

                Quote:

                Afzaal Ahmad Zeeshan wrote:

                go back to Haskell once again

                Yikes! :OMG:

                Trust me, Haskell is much easy and simple language. Although I started learning Haskell 2 days ago, I think I am enjoying the much simpler and better syntax. After all, you also like PHP[^]. ;-p Don't you Sander? :laugh:

                The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                Sander RosselS Offline
                Sander RosselS Offline
                Sander Rossel
                wrote on last edited by
                #13

                Afzaal Ahmad Zeeshan wrote:

                After all, you also like PHP[^]. ;-P Don't you Sander? :laugh:

                I really don't... :sigh: Although I use both in my blogs :) PHP: Web development #4: PHP in the back[^] Haskell: How to Learn and Polyglot vs. Specialist[^] Maths in IT #1: Basic set theory[^] Maths in IT #2: Venn diagrams[^] Yeah, that's quite some Haskell snippets! ;)

                Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                Regards, Sander

                A 1 Reply Last reply
                0
                • A Afzaal Ahmad Zeeshan

                  I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is a[5] == 5[a]?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:

                  int main()
                  {
                  int a[5] = {1, 2, 3, 4, 5};
                  for (int i = 0; i < 5; i++) {
                  std::cout << i[a] << ", ";
                  }
                  std::cout << std::endl;
                  system("pause");
                  return 0;
                  }
                  // Output: 1, 2, 3, 4, 5,

                  For those who didn't know it (just like me!) the logic is that C (or C++) translates a[5] to *(a + 5) and then gets the data from that location. Which, is similar to having 5[a] that gets translated to *(5 + a). Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].

                  The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #14

                  Good luck on the exam ! I find that code example frightening :)

                  Afzaal Ahmad Zeeshan wrote:

                  I can say I know nothing of C or C++

                  Well, you said the nothing you don't know well enough on this post to get my up-vote: that's something.

                  «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                  A 1 Reply Last reply
                  0
                  • A Afzaal Ahmad Zeeshan

                    I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is a[5] == 5[a]?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:

                    int main()
                    {
                    int a[5] = {1, 2, 3, 4, 5};
                    for (int i = 0; i < 5; i++) {
                    std::cout << i[a] << ", ";
                    }
                    std::cout << std::endl;
                    system("pause");
                    return 0;
                    }
                    // Output: 1, 2, 3, 4, 5,

                    For those who didn't know it (just like me!) the logic is that C (or C++) translates a[5] to *(a + 5) and then gets the data from that location. Which, is similar to having 5[a] that gets translated to *(5 + a). Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].

                    The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                    W Offline
                    W Offline
                    W Balboos GHB
                    wrote on last edited by
                    #15

                    Interesting - actually neat - but it would fail, I think if stretched a bit:

                    int x=5;

                    != x[a]; // I think: my work environments were just trashed

                    // by the server monkeys so I can't test.

                    But your discovery is truly wondrous!

                    "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                    "As far as we know, our computer has never had an undetected error." - Weisert

                    "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                    K 1 Reply Last reply
                    0
                    • W W Balboos GHB

                      Interesting - actually neat - but it would fail, I think if stretched a bit:

                      int x=5;

                      != x[a]; // I think: my work environments were just trashed

                      // by the server monkeys so I can't test.

                      But your discovery is truly wondrous!

                      "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                      "As far as we know, our computer has never had an undetected error." - Weisert

                      "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                      K Offline
                      K Offline
                      k5054
                      wrote on last edited by
                      #16

                      Seems to work fine.

                      #include <iostream>

                      int foo(int x, int *a)
                      {
                      return x[a];
                      }

                      int main()
                      {
                      int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                      int x = 5;

                      std::cout << arr\[x\] << " "
                                << x\[arr\] << " "
                                << foo(x, arr) << std::endl;
                      

                      }

                      Compiles and runs as expected without warning.

                      W 1 Reply Last reply
                      0
                      • K k5054

                        Seems to work fine.

                        #include <iostream>

                        int foo(int x, int *a)
                        {
                        return x[a];
                        }

                        int main()
                        {
                        int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                        int x = 5;

                        std::cout << arr\[x\] << " "
                                  << x\[arr\] << " "
                                  << foo(x, arr) << std::endl;
                        

                        }

                        Compiles and runs as expected without warning.

                        W Offline
                        W Offline
                        W Balboos GHB
                        wrote on last edited by
                        #17

                        That's unfortunate - I was hoping that the compiler wouldn't accept it because it makes x appear to be an array. It's not that I don't understand - but one could hope that the compiler would differentiate between the two.

                        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                        "As far as we know, our computer has never had an undetected error." - Weisert

                        "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                        1 Reply Last reply
                        0
                        • Sander RosselS Sander Rossel

                          Afzaal Ahmad Zeeshan wrote:

                          After all, you also like PHP[^]. ;-P Don't you Sander? :laugh:

                          I really don't... :sigh: Although I use both in my blogs :) PHP: Web development #4: PHP in the back[^] Haskell: How to Learn and Polyglot vs. Specialist[^] Maths in IT #1: Basic set theory[^] Maths in IT #2: Venn diagrams[^] Yeah, that's quite some Haskell snippets! ;)

                          Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                          Regards, Sander

                          A Offline
                          A Offline
                          Afzaal Ahmad Zeeshan
                          wrote on last edited by
                          #18

                          Much respect for you, to bear so much pain, Sander! :laugh: I have just started Haskell, and I am very much loving it. I would give these articles of your a look, you have written a great article for Haskell. You got some more I can pay some attention to? :-)

                          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                          Sander RosselS 1 Reply Last reply
                          0
                          • B BillWoodruff

                            Good luck on the exam ! I find that code example frightening :)

                            Afzaal Ahmad Zeeshan wrote:

                            I can say I know nothing of C or C++

                            Well, you said the nothing you don't know well enough on this post to get my up-vote: that's something.

                            «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                            A Offline
                            A Offline
                            Afzaal Ahmad Zeeshan
                            wrote on last edited by
                            #19

                            Thank you, Bill! But may I ask, it was "frightening" in the sense of?

                            The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                            B 1 Reply Last reply
                            0
                            • A Afzaal Ahmad Zeeshan

                              Thank you, Bill! But may I ask, it was "frightening" in the sense of?

                              The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                              B Offline
                              B Offline
                              BillWoodruff
                              wrote on last edited by
                              #20

                              Afzaal Ahmad Zeeshan wrote:

                              "frightening" in the sense of?

                              I'd say it's about the same way as I feel looking in a mirror: that sense that what is on the surface is a very bad cover-up of something strange, and probably up to no good :)

                              «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                              A 1 Reply Last reply
                              0
                              • A Afzaal Ahmad Zeeshan

                                Much respect for you, to bear so much pain, Sander! :laugh: I have just started Haskell, and I am very much loving it. I would give these articles of your a look, you have written a great article for Haskell. You got some more I can pay some attention to? :-)

                                The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                                Sander RosselS Offline
                                Sander RosselS Offline
                                Sander Rossel
                                wrote on last edited by
                                #21

                                Afzaal Ahmad Zeeshan wrote:

                                I have just started Haskell, and I am very much loving it.

                                Haskell has good parts. It certainly changed the way I do my C# programming :) I just don't see myself writing entire applications in Haskell just yet...

                                Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                Regards, Sander

                                A 1 Reply Last reply
                                0
                                • Sander RosselS Sander Rossel

                                  Afzaal Ahmad Zeeshan wrote:

                                  I have just started Haskell, and I am very much loving it.

                                  Haskell has good parts. It certainly changed the way I do my C# programming :) I just don't see myself writing entire applications in Haskell just yet...

                                  Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                  Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                  Regards, Sander

                                  A Offline
                                  A Offline
                                  Afzaal Ahmad Zeeshan
                                  wrote on last edited by
                                  #22

                                  Quote:

                                  I just don't see myself writing entire applications in Haskell just yet...

                                  Same thing here, I may never write entire application in Haskell, but I would love to use a few code blocks and make it interact with the WPF applications. :-)

                                  The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                                  Sander RosselS 1 Reply Last reply
                                  0
                                  • A Afzaal Ahmad Zeeshan

                                    Quote:

                                    I just don't see myself writing entire applications in Haskell just yet...

                                    Same thing here, I may never write entire application in Haskell, but I would love to use a few code blocks and make it interact with the WPF applications. :-)

                                    The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                                    Sander RosselS Offline
                                    Sander RosselS Offline
                                    Sander Rossel
                                    wrote on last edited by
                                    #23

                                    Why not use F#? That's a whole lot easier to integrate with your C# projects. You can even do WPF entirely in F# without any hassle/Haskell!

                                    Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                    Regards, Sander

                                    A 1 Reply Last reply
                                    0
                                    • Sander RosselS Sander Rossel

                                      Why not use F#? That's a whole lot easier to integrate with your C# projects. You can even do WPF entirely in F# without any hassle/Haskell!

                                      Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                      Regards, Sander

                                      A Offline
                                      A Offline
                                      Afzaal Ahmad Zeeshan
                                      wrote on last edited by
                                      #24

                                      I know F#, a little bit. But right now I am getting rid of being "All Microsoft Geek Boy!". I am learning Linux, trying out new flavors of Linux... Currently my choice is 1. Ubuntu Studio 2. Ubuntu 3. openSUSE So, when I was learning other operating systems, I thought why not learn other frameworks and programming paradigms. I am not learning Haskell for WPF as I am not at all developing WPF these days. I am instead learning functional programming itself. From my research I came to know that LISP, Scala etc. were all easier than Haskell, which provoked me to take Haskell as a challenge. It was a great experience learning Haskell. :-) No Microsoft based programming for a while... Learning Linux, learning non-Microsoft languages. I mean, there are many frameworks and systems that a programmer must know. After all, there are many already doing the job of "Jimmy Kode[^]" in this world. :laugh:

                                      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                                      Sander RosselS 1 Reply Last reply
                                      0
                                      • A Afzaal Ahmad Zeeshan

                                        I know F#, a little bit. But right now I am getting rid of being "All Microsoft Geek Boy!". I am learning Linux, trying out new flavors of Linux... Currently my choice is 1. Ubuntu Studio 2. Ubuntu 3. openSUSE So, when I was learning other operating systems, I thought why not learn other frameworks and programming paradigms. I am not learning Haskell for WPF as I am not at all developing WPF these days. I am instead learning functional programming itself. From my research I came to know that LISP, Scala etc. were all easier than Haskell, which provoked me to take Haskell as a challenge. It was a great experience learning Haskell. :-) No Microsoft based programming for a while... Learning Linux, learning non-Microsoft languages. I mean, there are many frameworks and systems that a programmer must know. After all, there are many already doing the job of "Jimmy Kode[^]" in this world. :laugh:

                                        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                                        Sander RosselS Offline
                                        Sander RosselS Offline
                                        Sander Rossel
                                        wrote on last edited by
                                        #25

                                        Afzaal Ahmad Zeeshan wrote:

                                        there are many frameworks and systems that a programmer must know

                                        The average programmer only needs to know a few (and knows less) :) It's good to know a bit about everything because every new language or framework gives you a new perspective on the ones you already knew. For example, knowing Haskell made me really appreciate C# again! :laugh:

                                        Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                        Regards, Sander

                                        A 1 Reply Last reply
                                        0
                                        • A Afzaal Ahmad Zeeshan

                                          I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is a[5] == 5[a]?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:

                                          int main()
                                          {
                                          int a[5] = {1, 2, 3, 4, 5};
                                          for (int i = 0; i < 5; i++) {
                                          std::cout << i[a] << ", ";
                                          }
                                          std::cout << std::endl;
                                          system("pause");
                                          return 0;
                                          }
                                          // Output: 1, 2, 3, 4, 5,

                                          For those who didn't know it (just like me!) the logic is that C (or C++) translates a[5] to *(a + 5) and then gets the data from that location. Which, is similar to having 5[a] that gets translated to *(5 + a). Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].

                                          The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                                          P Offline
                                          P Offline
                                          Plamen Dragiyski
                                          wrote on last edited by
                                          #26

                                          The compiler doesn't do a + 5, it is worse than that - it does a + 5 * sizeof(5), so 5[a] becomes 5 + a * sizeof(a), which is OK, since a is a pointer, and pointers are integers that can be added and multiplied. Learning something new about Haskell is like buying a new clothes. Learning something new about C/C++ is like discovering the nuclear reaction - it can change you and not always for good. Please, please, don't get twisted, don't go into C/C++ land! :)

                                          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