Last edited by std10093; 01-13-2013 at 05:03 PM.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
If you have a three dimensional array declared as `double A[x][y][z]' is it clearer to address an arbitrary element with indices a, b, c as
A[a][b][c] = foo;
or
*(((A+a*x*y)+b*x)+c) = foo;
? Especially when there are more than two dimensions, the bottom form becomes tedious and error prone.
Creating a function that operates on the array A will always require 4 parameters no matter if you use the pointer notation or the array notation: you need to pass one parameter representing the array A, and you need to pass three integer parameters representing the maximum bound of each dimension.
In other words, your choices for a function bar are pretty much just these two:
void bar1(int x, int y, int z, double *A);
void bar2(int x, int y, int z, double A[x][y][z]);
Last edited by c99tutorial; 01-14-2013 at 05:24 AM.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
No. Your example only considers single-dimensional arrays. With an n-dimensional array passed as input into a function, the compiler needs to "know" about the the size of the highest n-1 dimensions of the array. In addition, the programmer needs to know the size of the lowest dimension of the array as well as where the array begins in memory. So in total, you must design your function to accept n+1 parameters. This fact is true independent of whether you use the C99 syntax or C89 syntax. Using the C99 syntax simply lets you use indices inside the function in a natural mathematical way, i.e. "in the normal way".
Yes is the answer. When you want to pass a n dimensional array you pass n stars as pointers and n dimensions. So simple.
Also, I suppose you understood your mistake before... I guess actually it won't even compile. Tip : Compile the code before you post it
Enough said and this thread is solved.
Bye
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
files and sockets come to mind also.