Storage representation and implementation of multi-dimensional arrays-writing data structures by yourself
International practice, direct code
The multiarray. h file stores the data structure as follows:
# Ifndef _ MULTIARRAY_H _ # define MAX_DIM 10 # include
Typedef struct _ MArray {int * base; // base address int dim; // dimension int * bounds; // the header pointer that stores no dimension size int * wide; // header pointer for storing every dimension offset constant} MArray, * pMArray; pMArray init_multi_array (void); int set_multi_array (pMArray pma, int dim ,...); void destory_multi_array (pMArray pma); int locate_array_elem (pMArray pma, va_list ap, int * offset); int get_array_elem (int * e, pMArray pma ,...); int assign_array_elem (int e, pMArray pma ,...); # endif
The implementation method for storing the data structure in file multiarray. c is as follows:
/******************************** Time: 4.12.14 Author: XIAO_PING_PING compiling environment: DEV-C ++ 4.9.9.2 content: multi-dimensional array storage representation and implementation functions: learn about the data structure ********************************/# include
# Include
# Include "multiarray. h "/* Initialize an empty array */pMArray init_multi_array (void)/* Note: The output must be returned. Otherwise, an error is returned when a value is assigned to the member */{pMArray pma; pma = (MArray *) malloc (sizeof (MArray); pma-> base = NULL; pma-> bounds = NULL; pma-> wide = NULL; pma-> dim = 0; return pma;}/* set multi-dimensional array */int set_multi_array (pMArray p, int dims ,...) {int I, elem_num = 1; va_list ap = NULL; if (dims <1 | dims> MAX_DIM) {return-2;} p-> dim = dims; p-> bounds = (Int *) malloc (dims * sizeof (int); if (! P-> bounds) {return-1;} va_start (ap, dims); for (I = 0; I <dims; I ++) {p-> bounds [I] = va_arg (ap, int); if (p-> bounds [I] <0) {return-2 ;} elem_num * = p-> bounds [I];} va_end (ap); p-> base = (int *) malloc (elem_num * sizeof (int )); if (p-> base = NULL) {return-2;} p-> wide = (int *) malloc (dims * sizeof (int); if (! P-> wide) {return-1;} p-> wide [dims-1] = 1; for (I = dims-2; I> = 0; I --) {p-> wide [I] = p-> wide [I + 1] * p-> bounds [I + 1];} return 0 ;} /* destroy multi-dimensional array */void destory_multi_array (pMArray pma) {if (NULL! = Pma-> wide) {free (pma-> wide); pma-> wide = NULL;} if (NULL! = Pma-> bounds) {free (pma-> bounds); pma-> bounds = NULL;} if (NULL! = Pma-> base) {free (pma-> base); pma-> base = NULL;} free (pma );} /* positioning element a [I] [j] [k]... location */int locate_array_elem (pMArray pma, va_list ap, int * offset) {int I; int cur = 0; for (I = 0; I <pma-> dim; I ++) {cur = va_arg (ap, int); if (0> cur | cur> pma-> bounds [I]) {return-1 ;} * offset + = pma-> wide [I] * cur;} return 0;}/* Get the element at a specific position in the multi-dimensional array */int get_array_elem (int * e, pMArray pma ,...) {int I; int offset = 0; int Ret = 0; va_list ap = NULL; va_start (ap, pma); if (0! = Locate_array_elem (pma, ap, & offset) {return-1;} va_end (ap); * e = * (pma-> base + offset); return 0 ;} /* assign values to specific positions of multi-dimensional arrays */int assign_array_elem (int e, pMArray pma ,...) {int offset = 0; int ret = 0; va_list ap = NULL; va_start (ap, pma); ret = locate_array_elem (pma, ap, & offset); if (ret) {return-1;} va_end (ap); * (pma-> base + offset) = e; return 0 ;}
Test. c
# Include
# Include
# Include
# Include "multiarray. h "int main () {pMArray arr; int dim = 3; int bound1 = 2, bound2 = 3, bound3 = 4; // arr [2] [3] [4] array int I = 0, j = 0, k = 0; int assign_elem = 0; int get_elem = 0; int * p = NULL; arr = init_multi_array (); set_multi_array (arr, dim, bound1, bound2, bound3); printf ("array. bounds = "); // sequential output array. bounds p = arr-> bounds; for (I = 0; I <dim; I ++) {printf ("% d", * (p + I ));} printf ("\ narray. contents = "); // sequential output array. contents p = arr-> wide; for (I = 0; I <dim; I ++) {printf ("% d", * (p + I ));} printf ("\ narray [% d] [% d] [% d]: \ n", bound1, bound2, bound3); for (I = 0; I <bound1; I ++) {for (j = 0; j <bound2; j ++) {for (k = 0; k <bound3; k ++) {assign_elem = I * 100 + j * 10 + k; assign_array_elem (assign_elem, arr, I, j, k); get_array_elem (& get_elem, arr, I, j, k ); printf ("array [% d] [% d] [% d] = %-4d", I, j, k, get_elem );} printf ("\ n");} printf ("\ n");} p = arr-> base; for (I = 0; I <bound1 * bound2 * bound3; I ++) {printf ("%-4d", * (p + I); if (I % (bound2 * bound3) = bound2 * bound3-1) {printf ("\ n") ;}} destory_multi_array (arr); getch (); return 0 ;}
And the running result is as follows: