What would the prototype declaration of a C function look like that returns an n-dimensional array?
Here, n>=2.
Please explain it with dynamic-array/pointers.
My idea is not to pass any-dimensional array.
Please give me 3 examples with 2, 3 and 4 dimensions.
That will be enough for me to grab the idea.
I would suggest this:
where
arrayis an struct defined as:Of course,
datais just a pointer toint, but the other two fields in the struct can be used in such a way that thedatacan be interpreted like anndimensional array, and size of each dimension can be stored insizewhich is yet another pointer.For example, take 4-dimensional array (dimension size are
10,20,30and40), then you can create and initialize 4D array as:where
createfunction is defined as:Of course, you’ve to work a lot with the fields of the struct, to make it look like n-dimensional array. You don’t need to follow me exactly the way I explained, but this is just a basic idea. You can modify it, to suit your specific need.
I would suggest you to write few functions, to manipulate the array, and to access the array elements. I’ve written
createfunction, likewise here isdestroyfunction: