Articles

Affichage des articles du novembre, 2017

Does C++ have the array[x, y] syntax ?

The short answer is no. At least not out of the box. This happened a few months ago but I forgot to document it. I was reading the solutions of that [ Monday's r/dailyprogrammer challenge ] when I came across [ an elegant C++ solution ] that was posted by fellow user MrFluffyThePanda. Having written [ a C++ solution ] myself, I found it particularly interesting that MrFluffyThePanda's solution used a syntax I did not know C++ supported : int* field = new int[8,8]; The field array was later accessed with the field[x, y] syntax. The only language I know of that supports this syntax is C#, where they're referred to as "rectangular arrays". Was it one of those [ secret C++ gems ] that no one (read: me) knew about ? The answer was much simpler than that. My initial assumption was that the compiler translates field[x, y] into field[x * width + y], a method I sometimes use to avoid the headaches of working with multidimensional arrays in C. This was suggested by