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 :). -
It's all goodYou may call me char 71 97 100 100 97 109.
-
-
Might as well throw in my solution as well, heh.
Source Code
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:
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:
-
James is that XOR your using?
K - excellent points! no pun intended.You may call me char 71 97 100 100 97 109. -
Yes, the '^' is XOR (or at least I remember that being it, I don't use it much...)
en.wikipedia.org/wiki/XOR_swap_algorithm
Also I just want to agree with what Rez said and further reinforce what I said earlier: Don't try to do pointer arithmetic with things that aren't arrays, as there is no guaranteed order that things will appear in the stack or in the heap.
James -
-
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
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 -
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 -
I did some research on the XOR swap and it seems that 99.9% of programmers also agree with you Rez. I'm glad I spotted that it was using XOR though so I'm pleased with myselfYou may call me char 71 97 100 100 97 109.
-
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
The only other reason I mentioned it was because someone showed me a sample interview question once where you had to answer what that actually did.
James -
Oh sweet! I'll know that answer for sure now!You may call me char 71 97 100 100 97 109.
-
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
-
Share
- Facebook 0
- Twitter 0
- Google Plus 0
- Reddit 0
-
Users Online 1
1 Guest