There are a couple of issues you have to be aware of here.
Here's the first one. If you want to pass a string to a function (say, the filename), then try something like this.
Code:
void function(const char *filename) {
/* ... */
}
function("file.txt");
const char *file2 = "file2.txt";
function(file2);
Notice how if you're passing in a char* (or a char[]) to a function, you just use the name of the variable. Here's an example in three dimensions.
Code:
void threeD(int array[][2][2]) {
/* ... */
}
int data[2][2][2] = {
{ {1, 2, 3}, {4, 5, 6} },
{ {7, 8, 9}, {10, 11, 12} }
};
threeD(data);
The last line can be written, equivalently, as
if you prefer.
There's an important point hidden in that little example. When you pass a multi-dimensional array to a function, you have to specify in the function prototype (or definition) the size of every dimension of the array, except for the leftmost dimension. So to pass my 3D array of type int [2][2][2] to the function, I had to say "int array[][2][2]" in the function itself.
There's a reason for this. In C, even if you say you want a multidimensional array, beneath the hood it's really just a one-dimensional chunk of contiguous memory. Inside a calling function, if you have a three-dimensional array and you ask for array[1][1][1], the code has to calculate the offset into this chunk of memory. The array is laid out logically, with array[0][0][0] being the first element, then array[0][0][1], array[0][0][2], and so on; then comes array[0][1][0], array[0][1][1], etc. In other words, if you want to calculate where array[1][?][?] is, you have to know how many elements are in the rightmost column and how many elements are in the next rightmost column. Because array[1][0][0] is one element after array[0][N][M], where N and M are as big as they are allowed to be.
That's wasn't a very good explanation, I don't think. Oh well. The upshot is that because all a function is passed is the address of the beginning of the array, it has to know the size of most of the array's dimensions to calculate offsets into it.
I guess this won't really be a problem for you, since you have hard-coded values here:
Code:
unsigned int array[200][200][10];
But if you're trying to pass an array which can vary in size to a function, it's inconvenient to have to specify the dimensions in the function itself. If the dimensions are different for a different array, well, you can't use that array. There are a few ways to get around this, such as "flattening" the array into a one-dimensional array and calculating the offsets yourself with size parameters passed into the function, but I won't go into it because you probably wouldn't be interested.
Anyway, that's almost enough information to do what you want to do. The other important point is that you can't return arrays. Sorry, that's just the way it is. I guess the language designers thought it was too inefficient. The easiest way to get around this is to pass a pointer to the array you want to fill into the function. For example:
Code:
void zero_everything(int (*data)[2][2]) {
int x, y;
for(x = 0; x < 2; x ++) {
for(y = 0; y < 2; y ++) {
(*data)[x][y] = 0;
}
}
}
Notice the syntax I've used; make sure you use the parentheses as I have it. If you leave them out the code does something different, due to operator precedence.
So . . . yeah. Here's a skeleton. Read the above, adopt this, and have fun.
Code:
void read_data(const char *file, unsigned int (*array)[][200][10]) {
/* ... */
/* We use *array here because that gives us a pointer to the beginning of a three-dimensional array,
which is what we want.
*/
fread(*array, sizeof(unsigned char), filesize, file);
/* ... */
}
/* in some other function . . . */
unsigned int data[200][200][10];
char filename[BUFSIZ], *p;
fgets(filename, sizeof filename, stdin);
if((p = strchr(filename, '\n'))) {
*p = 0; /* remove newline */
}
open_file(filename, data);