What is the difference (or link) between:
Case for 1D arrays:
arr + i
andCase for 2D arrays:
(*(arr + i) + j)
andWhen should I use one or the other, or are they equivalent?
For 1D arrays:
Element address using pointers:
Element address = base address + element size * element number:
For 2D arrays:
Address using pointers: *(arr + i) + j:
Element address = base address + element size * ((row number * total number of columns) + column number):
In general, the pointer-based approaches (arr + i and *(arr + i) + j) are more commonly used and efficient, as they directly leverage the underlying memory layout of the arrays. However, the explicit address calculation can be useful in certain scenarios, such as when working with variable-sized elements or when the array dimensions are not known at compile-time.