Sooner or later, any developer working with C-like languages gets the idea of treating a two-dimensional array as a one-dimensional one. The reasons vary, but the result is usually the same. In this...
what kind of psychopath even came up with int a[ROWS][COLS] = { 0, 1, 2, 3, 4, 5, 6, 7 };
it’s even obviously caught by -Wall (-Wmissing-braces) for both clang and gcc
(oh, actually, g++ fails to recognize it even though gcc and clang do recognize it)
sometimes the latter part is also caught by -fsanitize=undefined, though that goes away if you wrap the array access like so: printf("%d\n", ((int*)a[0])[i]); (which I’m unsure if that’s still undefined behavior, not that it’s any more sane even if it isn’t)
what kind of psychopath even came up with
int a[ROWS][COLS] = { 0, 1, 2, 3, 4, 5, 6, 7 };
it’s even obviously caught by -Wall (-Wmissing-braces) for both clang and gcc
(oh, actually, g++ fails to recognize it even though gcc and clang do recognize it)
sometimes the latter part is also caught by -fsanitize=undefined, though that goes away if you wrap the array access like so:
printf("%d\n", ((int*)a[0])[i]);
(which I’m unsure if that’s still undefined behavior, not that it’s any more sane even if it isn’t)I came from C, but sadly haven’t needed using 2 dimensional arrays enough to be able to say something useful.
I tend to use
std::array
andstd::vector
anyway.But definitely no linear initialisation. It’s already too hard to remember which pointers the first
[]
vs second[]
refer to.