#include /* Utilities for static and dynamic two-dimensional arrays in C Static arrays: Stored as blocks in memory in "row-major" order. That is, given int A[3][3], the data is laid out in memory as: A[0][0], A[0][1], A[0][2], A[1][0], A[1][1], etc ... You need to be aware of this when makeing pointer calculations. "Static" means that memory for the array is allocated at compile time - you must know the dimensions of your array when declaring them. You can only use constants or macros for constants when stating the sizes of arrays. With static arrays it's easy to calculate the array index, and you can send/receive an entire matrix using a single block transfer. But you do need to know your pointers. Given the declaration double A[3][3], what is A+2? You might think that it's the address of element A[0][2], the third element. But that's not correct. Each row consists of 3 elements, so A+2 should really be thought of as A+(2*3), which is the address of the third row, or of A[2][0]. Got it? OK, but how do you get the address of A[0][2] then? You can use A[0]+2, or type cast A from type double[][] to double*: ((double*)A)+2 will give you the address of the 2nd double value after the memory address A. Pointer arithemetics depend on type info. Another thing to remember is that very large arrays should not be allocated on the stack, as that will overflow the stack. That is, instead of { double A[1000][1000]; Do instead: static double A[1000][1000]; Or, if this is the principal structure of your program, you can declare it as a global (but don't get carried away with globals!). Declaring the variable static means that all manifestations of the current scope (function call) will share the same structure. However, you should understand the following if you declare a static or global static array, the memory will be allocated for the array regardless of whether the program really needs it. Another not-so-nice thing about static arrays is that, when defining a function, you must specify the array dimensions (at least columns if not rows), otherwise there'll be no way for the function to calculate the correct indices. But then how do you write generic procedures to handle arrays of any size? You can use dynamic arrays (below), or macros such as follows. Macros are applied to your code before the compiler does any real work - it's just a form of automatic cutting and pasting. Specifically, parameters to macros are dynamically scoped, so be careful that the names of these params are separate from all others. */ // for static, multi-dimensional arrays in C, sometimes a macro is better // than a function. Note that i, j are still local vars because they're // inside {}'s. // prints array AA, size rzXcz, using formated string fs. // use as in PRINT2M(A,4,5,"%f ") #define PRINT2M(AA,rz,cz,fs) { \ int i, j; \ for (i=0;i