I’m looking for a shortcut for 2 and 3 dimensional indexing of arbitrary values, and I know this works for a given array a[]
#define a(i,j,k) a[(i)*span*span+(j)*span+(k)]
#define b(i,j) b[(i)*span+(j)]
But I don’t understand how to allow these macros to operate on arbitrary arrays eg;
x(i,j,k)
Anyone care to clue me in?
Don’t define the macro name to
aorb, instead, give the array as parameter:Then
aandbwill be parameters and you’ll be able to use it for different arrays.Edit
For example:
char newarr[5][5][5];
arr3d(newarr, 3, 3, 3);
also, if
spanis not a variable defined in all scopes where you need to use this macro, it will not work, and you’ll need to add another parameter to the macro – span, so the macro definition will look like:And yes, I would usually avoid this.