Thread: Passing array of arbitrary dimension

1. Passing array of arbitrary dimension

Say I want to create a function which finds the highest number in an array of arbitrary dimension, how would I construct the parameters to receive such a variable?

I don't want to create max functions for each of the following:

Code:
```double max(double *num, int length) {...}
double max2d(double **num,  int length) {...}
double max3d(double ***num, int length) {...}```
etc...

That would be painful. I'd rather have a universal function that takes an array of any dimension, along with the number of the dimension itself.

2. Well, you could use a little trickery. Make a function that takes a pointer and the number of dimensions. Then cast it to correct type inside function. Be sure to cast the pointer to a single pointer when passing.
I don't know of any other clean way.

3. No need to cast, just use pointer arithmetic. Assuming of course each dimension has the same number of elements.

4. I'm not completely sure this will work correctly as its not something I have done before, but could you not cast the pointer type to a 1d array.
Code:
```#include <stdio.h>
void foo(int *array, int dimensions)
{
printf("&#37;u\n", dimensions);
}

int main()
{
int a[100];
int b[10][10];
foo((int*)a, 1);
foo((int*)b, 2);
return 0;
}```
This shoudl result in an array that is effectivly flattened within your function so if your dimensions vary in size you may have to handle this somehow.

Edit:
No need to cast, just use pointer arithmetic.
Maybe am missing something here but it wont compile for me unles I cast it.

5. >I'd rather have a universal function that takes an array of any dimension, along with the number of the dimension itself.
Perhaps a void *?
Code:
`double max(void *num, int num_dimensions, int length) {...}`

6. Originally Posted by mike_g
Maybe am missing something here but it wont compile for me unles I cast it.
Pass a pointer to the array. It doesn't matter how many dimensions it has, arrays are contiguous.

7. Unless it's dynamically allocated, which could pose a problem.

8. Yes, but he didn't say it was, still the same thing really -- just follow the pointers.

9. Pass a pointer to the array. It doesn't matter how many dimensions it has, arrays are contiguous.
Well I know that arrays are contiguous On second inspection it was only a warning that gcc gave, but if I remember correctly VS wont compile unless you cast it, and tbh I'd rather not see any unnecessary warnings when i compile. Makes the real problems harder to find.

10. Originally Posted by mike_g
...but if I remember correctly VS wont compile unless you cast it, and tbh I'd rather not see any unnecessary warnings when i compile. Makes the real problems harder to find.
That's because it's bad if it compiles.

And I do get the feeling it's dynamic looking at the function prototypes.

11. Code:
```#include <stdio.h>

double max(double * start, size_t length);

int main(void)
{
double a[] = {0.5, 20.4, 158.1};

double b[][3] = {
{0.5, 20.4, 158.1},
{0.785, 1450.4, 1258.1},
{0.9, 254.68, 14.1}
};

double c[][3][3] =  {
{
{0.5, 360.4, 4758.1},
{0.4755, 2047.4, 1548.1},
{0.5, 270.44, 158.145}
},

{
{47.5, 2470.4, 15478.1},
{470.5, 2440.4, 15847.1},
{0.75, 2470.47, 14758.1}
}
};

printf("max of a = &#37;f\n", max(&a[0], 3));
printf("max of b = %f\n", max(&b[0][0], 9));
printf("max of c = %f\n", max(&c[0][0][0], 18));
return 0;
}

double max(double * start, size_t length)
{
double m = start[0];
size_t i = 0;

for(i = 0; i < length; i++)
{
if(start[i] > m)
m = start[i];
}

return m;
}```
Ofcourse if it's DMA'd then you'd need a more robust solution

12. I never expected it to be dynamically assigned. Would cause too much problems for something like this. But it is a possibility, youre right.

As for using a void pointer, that might be good, but it dosent say anythin about the data type. Personally I never used void pointers myself yet, maybe I should look into it.

13. Perhaps try map each pointer to a dimension (with how many elements it contains and whatever). Pass the map as an array (one map per dimension).

I don't know if that's the best way to do it