In what example situation I should use the trickier form of it? Does it generate a faster binary code? If I can do the job with the [][] notation, should I still make an extra effort for some reason and implement it with pointers?
For single data I understand the need of pointers. I am asking specifically for two dimensional arrays and not about the general sense of using pointers.
In what example situation I should use the trickier form of it?
Never, unless you're attending a code obfuscation contest as mentioned in @M.M' comment
Does it generate a faster binary code?
No, the emitted assembly instructions should be the same with any decent compiler.
should I still make an extra effort for some reason and implement it with pointers?
No. Use standard containers or smart pointers instead. Refrain to use raw c-style arrays and pointers.
For known sizes the simplest way is
std::array<std::array<T,5>,10> array2D;
For 2D arrays variying in size you can use
size_t rows, columns;
std::cout << "Enter row and column size please > " << std::flush;
if(std::cin >> rows >> columns) {
std::vector<std::vector<T>> array2D(rows,std::vector<T>(columns));
}
To optimize 2D arrays to use contiguous dynamic memory blocks, consider to write a small wrapper class, that translates row and column offsets into the internal value positions (e.g. maintained in a simple std::vector
).