International practice, directly on the code
The file multiarray.h holds the data structure body as follows:
#ifndef _multiarray_h_#define _multiarray_h_#define Max_dim 10#include <stdarg.h> typedef struct _marray{ int *base; Base address int Dim; Dimension int *bounds; A head pointer with no dimension size int *wide; The head pointer that holds each 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
file MULTIARRAY.C Storage data structure implementation method, as follows:
/******************************* Time: 2014.12.14xiao_ping_ping compilation environment: dev-c++ 4.9.9.2 Content: Storage representation and implementation of multidimensional arrays: Learn some data structures ******* /#include <string.h> #include <stdlib.h> #include "multiarray.h"/* Initialize an empty array */ Pmarray Init_multi_array (void)/* Note: The return value must be output, otherwise the value assigned to the member will be error */{Pmarray PMA; PMA = (Marray *) malloc (sizeof (Marray)); Pma->base = NULL; Pma->bounds = NULL; Pma->wide = NULL; Pma->dim = 0; return PMA;} /* Set multidimensional 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 multidimensional arrays */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);} /* anchor element A[i][j][k] ... Position */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 element */int Get_array_elem (int* e,pmarray PMA,...) for a multidimensional array-specific location {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 a value to a multidimensional array-specific location */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 File test.c
#include <conio.h> #include <stdlib.h> #include <string.h> #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 < bo Und3; k++) {Assign_elem = i * + 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 results of the operation are as follows:
Storage representation and implementation of multidimensional array--writing data structure by itself