多維數組節點,有四個元素
struct array
{
int *base; //存放數組的元素的基地址
int dim; //表示多維數組的維數
int *bounds; //表示每一維的長度
int *constants; //存放數組映象函數常量基址
};
譬如一個2*3的二維數組,dim就等於二,行優先存儲,bounds[0]就為2,bounds[1]就為3。
對於constants,constant[i]就是第i+1層的數組中每一元素(數組)的大小。
對於行主序的2*3的二維數組來說,constant[1]就是每一行的一個元素(如:((a,b,c),(d,e,f))中a,f)占據的內存長度
constant[0]就是當前行每一列(如:(A,B),其中A=(a,b,c),B=(d,e,f)),A、B占據的內存長度。
- /*
- 多維數組的順序表示
- 調試環境:vs2010,gcc
- */
-
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <malloc.h>
-
- #define OK 1
- #define ERROR 0
- #define MAX_ARRAY_DIM 8
-
- typedef struct
- {
- int *base; //數組的基地址,初始化時分配
- int dim; //數組的維數
- int *bounds; //數組每一維的長度
- int *constants; //數組映象函數常量基址
-
- }array;
-
- int init_array(array *a, int dim, ...)
- {
- int i = 0;
- int elem_num = 1; //記錄數組中元素的個數
- va_list ap = NULL;
-
- if(dim < 1 || dim > MAX_ARRAY_DIM)
- {
- return ERROR;
- }
- a->dim = dim;
- a->bounds = (int *)malloc(dim * sizeof(int));
- if(!(a->bounds))
- {
- return ERROR;
- }
-
- va_start(ap, dim);
- for(i = 0; i < dim; i++)
- {
- a->bounds[i] = va_arg(ap, int);
- if(a->bounds[i] < 0)
- {
- return ERROR;
- }
- elem_num *= a->bounds[i];
- }
- va_end(ap);
-
- a->base = (int *)malloc(elem_num * sizeof(int));
- if(! (a->base))
- {
- return ERROR;
- }
-
- a->constants = (int *)malloc(dim * sizeof(int));
- if(!(a->constants))
- {
- return ERROR;
- }
- a->constants[dim - 1] = 1;
- for(i=dim-2; i>=0; i--)
- {
- a->constants[i] = a->constants[i+1] * a->bounds[i+1];
- }
-
- return OK;
- }
-
- int destory_array(array *a)
- {
- if(a->base)
- {
- free(a->base);
- a->base = NULL;
- }
- else
- return ERROR;
-
- if(a->bounds)
- {
- free(a->bounds);
- a->bounds = NULL;
- }
- else
- return ERROR;
-
- if(a->constants)
- {
- free(a->constants);
- a->constants = NULL;
- }
- else
- return ERROR;
-
- return OK;
- }
-
- /*尋找待搜索的arr[i][j][k]相對於基地址的偏移量*/
- int locate_array_elem(array a, va_list ap, int *offset)
- {
- int i = 0;
- int curdim = 0;
-
- for(i = 0; i < a.dim; i++)
- {
- curdim = va_arg(ap, int);
- if(curdim < 0 || curdim >=a.bounds[i])
- {
- return ERROR;
- }
- *offset += a.constants[i] * curdim;
- }
-
- return OK;
- }
-
- int get_array_elem(int *e, array a, ...)
- {
- va_list ap;
- int result = 0;
- int offset = 0;
-
- va_start(ap, a);
- if(!(result = locate_array_elem(a, ap, &offset)))
- {
- return ERROR;
- }
- va_end(ap);
-
- *e = *(a.base + offset);
-
- return OK;
- }
-
- int assign_array_elem(int e, array *a, ...)
- {
- va_list ap;
- int result = 0;
- int offset = 0;
- va_start(ap, a);
- if(!(result = locate_array_elem(*a, ap, &offset)))
- {
- return ERROR;
- }
- va_end(ap);
-
- *(a->base + offset) = e;
-
- return OK;
- }
-
-
- int main(int argc, char *argv[])
- {
- array arr;
- int dim = 3;
- int bound1 = 2, bound2 = 3, bound3 = 4; //arr[2][3][4]數組
- int i = 0, j = 0, k = 0;
- int assign_elem = 0;
- int get_elem = 0;
- int *p = NULL;
-
- init_array(&arr, dim, bound1, bound2, bound3);
-
- printf("array.bounds = "); //順序輸出array.bounds
- p = arr.bounds;
- for(i = 0; i < dim; i++)
- {
- printf("%d ", *(p + i));
- }
-
- printf("\narray.contents = "); //順序輸出array.contents
- p = arr.constants;
- 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_array(&arr);
-
- return 0;
- }