On the indexing of arrays and other little wonders
-
We start counting at zero and the rest of humanity start with 1
I would argue the rest of humanity is wrong ... but then I don't have the support to enforce a new counting. ;)
I do have this case with PLCs.
I've said it before and I'll say it again: don't use substandard controllers and/or compilers ... ;)
on c++ is also [X] on PLC and
not [X+1]Not only for debugging. I'd say you're just fine wasting a single element from the beginning instead of making a "minor" mistake changing offsets ... however I'm sure you'd agree that the OP's justification for doing that is somewhat iffy ...
-
@mrjj said:
And this is why normal people and programmer always talk past each other.
We start counting at zero and the rest of humanity start with 1 :)Actually, my hardware design professor (FPGAs and stuff) can't stand it if I/we start to enumerate with 0 instead of 1. Yes sure, one can now pull the argument whether people doing hardware design using tools like VHDL and Verilog are actually programmers but... do they count as "normal people"? :p
It's just funny to see that a very decent engineer/professor doesn't like enumeration to start with zero.
Next time I'll have that discussion with him (which is surprisingly often), I'll pull the argument that @kshegunov stated: It's the offset of the element :pMaybe we get too off-topic now.
-
- Maybe we get too off-topic now
-
That would probably be rather rude for the OP :p
-
@Joel-Bodenmann said:
It's just funny to see that a very decent engineer/professor doesn't like enumeration to start with zero.
Everyone has the right to have no taste ...
I'll pull the argument that @kshegunov stated: It's the offset of the element :p
I thought this was common knowledge.
How is this:
int x[5]; x[2] = 0;
different from:
*(x + 2) = 0;
PS: Except for the extreme ugliness of the latter that is ... :)
-
@kshegunov This is probably the point where I can dump this one...
#include <iostream> #include <string> int main() { std::string a[] = {"Hello", "World"}; std::cout << 0[a] << std::endl; std::cout << 1[a] << std::endl; return 0; }
This proves that
operator[]
is really nothing but an offset operator (well, one can overload it for classes, yes....). -
Man, this:
std::cout << 0[a] << std::endl;
shouldn't work even on Friday ...
By the way, the proper way to unnecessarily make things look unfathomable is to use fully qualified access:
std::cout << a.operator[] (0) << std::endl;
:)
-
@kshegunov said:
By the way, the proper way to unnecessarily make things look unfathomable is to use fully qualified access:
std::cout << a.operator[] (0) << std::endl;
Well, that won't work with plain arrays ;)
Also you can't annoy C people with that. -
@Joel-Bodenmann
You youngsters and your requirements ... plain arrays are plain, they need no stinkin' (custom) operators in the first place ... :DAlso you can't annoy C people with that.
With this I agree completely, but they can still annoy you by naming their variables
new
, like this:char new[5]; //< This always makes my day
-
@kshegunov
What about#define float int
? :p
If you hide stuff like this:#define if(x)
you can truly fuck someone's day.However, the most evil one might be this:
#define struct union
. -
-
@the_ said:
public class if { }
Ouch! There's tons of weird stuff a C dev might do, like:
struct class object;
Aha! We are bringing the heavy artillery, aren't we?
Since the preprocessor is not part of the language, and it's run before anything else ... and it's just string substitution, you can make very ugly stuff, like:
#define true 0 #define false 1
-
Aha! We are bringing the heavy artillery, aren't we?
No Sir, this is the heavy artillery:
#define if(x) if((__LINE__ % > 500)^(x))
Everything works well as long as the source doesn't grow larger than 500 lines.
And if you want to keep people from fucking with your sources/algorithms, you can carefully write a program with this:
#define if(x) if((__LINE__ % 5==0)^(x))
If done right, your stuff will work fine for as long as nobody adds or removes a line.
</topic>
-
This is (somewhat) tractable. My greatest clash with the preprocessor was when I was served with a "class" that defined a 256 item color table by means of members for each of 256 colors ... the properties, getters and setters were naturally defined through preprocessor macros. It was one of the low points of my life ... :)
-
@kshegunov
You might like my embedded graphics/GUI library then. Everything that can be done with preprocessor macros is done using preprocessor macros:https://bitbucket.org/Tectu/ugfx/src/fb100bcc25225cfad0bab5d40bef1db703e9670a/src/gdisp/gdisp_colors.h?at=master&fileviewer=file-view-default
https://bitbucket.org/Tectu/ugfx/src/fb100bcc25225cfad0bab5d40bef1db703e9670a/src/gdisp/gdisp_driver.h?at=master&fileviewer=file-view-default#gdisp_driver.h-812If you keep browsing the sources you will find other goodies :)