Pointer notation is confusing.

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Originally posted by rezination
      Sorry man, but it's not pretty at all, it's extremely hard to read. This is what we call "clever" code. It's code that is trying to do something clever without actually adding any value.

      One of the most important things you can do with code is communicate intent. If I saw that in the code base, I would be confused as to what you were doing and why. This is MUCH more elegant and easier to read:
      -Rez


      Thanks Rez. I see your point. I just took a note for myself for being more explicit and think about other developers:).
      My intuition for writing such a code was: In mathematics, vector is just an "array" of number. Most of the time I use subscript operator to get a value from it. But this is not very explicit and may not make any sense for other programmers. Also vectors are seems to be used with their geometrical interpretation in video games not mathematical. Anyways this is not really related to subject.
      My second lazy intuition was I hate writing get/set methods.:)

      @Gaddam
      I am sorry for posting additional questions on your thread. But I guess somehow they are related to pointers. :)

      @kaykry
      I am also sorry for bugging your head with this silly question, and making you waste your time :).
    • Might as well throw in my solution as well, heh.

      Source Code

      1. void ReverseString(char * str, size_t strSize)
      2. {
      3. char * begin = str;
      4. char * end = &str[strSize - 2];
      5. while (begin < end)
      6. {
      7. char value = *end;
      8. *end = *begin;
      9. *begin = value;
      10. ++begin;
      11. --end;
      12. }
      13. }
      Display All


      BTW, there is a potential gotcha hidden in this question. strlen(testString) + 1 is the total length of the string including the null termination. But since we don't want to move the null termination the actual end of the string (the 'y') is equal to &str[strSize - 2], since we start at index 0 not 1.

      Off by one error

      Also:

      Source Code

      1. char * testString = "bunny";

      gets compiled as a constant expression, and so trying to write to any section of the string will result in a "Access violation writing location" error. A solution is to declare the string like so:

      Source Code

      1. char testString[] = "bunny";
    • Yep. In my Lua post I showed a fun example of this. For example, take this code where I'm squaring a number several million times:

      Source Code

      1. int Square(int val)
      2. {
      3. return val * val;
      4. }
      5. void RunPerfTest(void)
      6. {
      7. int total = 0;
      8. // start profiling here
      9. for (int i = 0; i < 20000000; ++i)
      10. {
      11. total += Square(i);
      12. }
      13. // end profiling here
      14. }
      Display All


      How long do you think that loop will take? In release mode, it will take no time at all, the compiler will completely strip it out. It realizes no meaningful state has changed after this loop.

      Trust your compiler. :)

      -Rez
    • Originally posted by rickvanprim

      Source Code

      1. void ReverseString(char* str, size_t strSize)
      2. {
      3. for(int i = 0; i < strSize / 2; i++)
      4. {
      5. int j = strSize - (i + 1);
      6. str[i] = str[i] ^ str[j];
      7. str[j] = str[i] ^ str[j];
      8. str[i] = str[i] ^ str[j];
      9. }
      10. }


      :)

      James

      Thanks for posting this. The XOR swap is a perfect example of "clever" code that you should never do. It's not as readable as using a temp variable and it's usually slower. This is because the CPU can't run the XOR's in parallel. In fact, some compilers can completely optimize away the swap and generate no machine code at all.

      en.wikipedia.org/wiki/XOR_swap…for_avoidance_in_practice

      So yeah, you should never use an XOR swap. Sorry James. :(

      -Rez
    • Originally posted by rickvanprim
      Yes, that's why my entire post was just that example and a smiley face. It was the "ha ha look at how clever I am" post ;)

      No worries, I didn't mean to call you out. I just wanted to make sure it was clear to everyone that it wasn't the correct solution and why. Besides, you and I are friends enough that I know I can jab at you a bit. ;)

      -Rez