多维数组节点,有四个元素
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;
}