answersLogoWhite

0


Best Answer

You cannot return an array by value as all arrays (including multi-dimensional arrays) will implicitly degrade to pointers, so they must be returned by reference instead. However, you cannot return the address of a local variable as that variable will fall from scope when the function returns, so the memory is no longer valid. Therefore the array must be allocated dynamically, on the heap.

You cannot create multi-dimensional arrays on the heap as easily as you can on the stack or in static memory. Essentially you need three separate arrays. The first holds all the actual elements and is allocated by multiplying all the dimensions together with the size of each element. Thus to dynamically allocate the equivalent of a char[2][3][4] array, you would actually allocate a char[24] array (2*3*4=24).

char* array1 = malloc (2*3*4*sizeof(char));

The second array holds pointers into the first array. This array is the equivalent of a char*[2][3] array, but is actually allocated as a char*[6] array (2*3=6):

char** array2 = malloc (6*sizeof(char*));

Once you have this array you need to initialise the pointers so they each point into the first array, where each pointer refers to the start address of a group of 4 elements:

for (int i=0; i<6; ++i) {

array2[i]=&array1[i*4];

}

Finally, you can allocate the third array which holds pointers into the second array and is equivalent to a char**[2] array:

char*** array3 = malloc (2*sizeof(char**));

Again, the pointers must be initialised to point into the second array, where each pointer points to the start address of a group of 3 pointers:

for (int i=0; i<2; ++i) {

array3[i]=&array2[i*3];

}

Putting this all together, we can create a function that dynamically allocates a three-dimensional char array of any size:

char*** create_array3d (unsigned d1, unsigned d2, unsigned d3) {

int i;

char* a1 = malloc (d1*d2*d3*sizeof(char));

char** a2 = malloc (d1*d2*sizeof(char*));

char*** a3 = malloc (d1*sizeof(char**));

for (i=0; i<(d1*d2); ++i) {

a2[i]=&a1[i*d3];

}

for (i=0; i<d1; ++i) {

a3[i]=&a3[i*d2];

}

return a3;

}

Thus the signature for this function will be:

char*** create_array3d (unsigned, unsigned, unsigned);

The problem with this function is how we go about releasing the memory given we only have one pointer. The easiest way is to create another function:

void release_array3d (char*** ptr) {

free (ptr[0][0];)

free (ptr[0];

free (ptr);

}

To demonstrate how we might use these functions, consider the following:

int main (void) {

const int d1=2;

const int d2=3;

const int d3=4;

int x, y, z;

/* instantiate array */

char*** array = create_array3d (d1, d2, d3);

/* initialise it with some values */

for (x=0; x<d1; ++x)

for (y=0; y<d2; ++y)

for (z=0; z<d3; ++z)

array[x][y][z] = (x+1)*(y+1)*(z+1);

/* perform other operations on array */

/* ... */

/* release array */

release_array3d (array);

array = 0;

return 0;

}

Note how we can access the array elements just as if the array were allocated on the stack or in static memory and that the functions hide all the (low-level) implementation details from the user.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What will be the signature of the function that returns three-dimensional array char arr234?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions