C語言 數組的順序表示與實現 數據結構
今天下午琢磨c語言中的數組的存儲結構,特別是多維數組的存儲結構,結合以前學習的彙編語言,對多維數組
在存儲單位中的分配情況,書上的數據結構中的說明有些代碼我實在是沒看明白,但是我還是努力地把代碼,寫
了下來,雖然代碼不能運行,但是也大致地體現了,多維數組在內存機制中調用方式。
好啦,還是和大家一起分享,如果大家對代碼有什麼看法或觀點,可以發送郵件到[email protected],我會
儘快給您回覆的。
/****************************************/ /*Description: Sequence Array*/ /*Email:[email protected]*/ /*Author:yi_landry Harbin Normal University Computer Science*/ /*Date:2008-5-2*/ /*Copyright:HNU2008.cop*/ /*Environment:turbo c 2.01 English Version*/ /****************************************/ # include<stdlib.h> # include<stdio.h> # include<stdarg.h>/*afford macro va_start,va_arg_arg and va_end*/ # define OVERFLOW 0 # define OK 1 # define MAX_ARRAY_DIM 8 //The largest dimension of the sequence array /****************************************/ /*The struct of sequence array */ /****************************************/ struct SqArray { int * base;/*the base address of the sequence array*/ int dim; /*the dimension of the sequence array*/ int * bounds;/*the base address of dimension bound*/ int * constants;/*the base address of mapping function*/ int elemtotal;/*the total elements of in the sequence array*/ }A; /****************************************/ /*Initial the array*/ /****************************************/ InitArray(struct SqArray * A,int dim,va_list ap)/*va_list is a list type can contain variable elements*/ { int i; int p = dim; if (dim <1 || dim > MAX_ARRAY_DIM) { printf("The dimension %d you input is overflow!/n",dim); return 0; } A->dim = dim; A->bounds = (int *)malloc(dim *sizeof(int)); if (!A->bounds) exit(OVERFLOW); A->elemtotal = 1; va_start(ap,dim); for (i=0;i<dim;++i) { A->bounds[i] = va_arg(ap,int); if (A->bounds[i]<0) exit(OVERFLOW); A->elemtotal *= A->bounds[i]; } va_end(ap); A->base = (int *)malloc(A->elemtotal * sizeof(int)); if (!A->base) exit(OVERFLOW); A->constants = (int *)malloc(dim* sizeof(int)); if (!A->constants) A->constants[dim-1]=1; for(i = dim-2;i>=0;--i) A->constants[i] = A->bounds[i+1]*A->constants[i+1]; return OK; } /****************************************/ /*Destroy the current sequence array*/ /****************************************/ Destrory(struct SqArray * A) { if (!A->base) return 0; free(A->base); A->base = NULL; if (!A->bounds) return 0; free(A->bounds); A->bounds = NULL; if (!A->constants); return 0; free(A->constants); A->constants =NULL; return 0; } /****************************************/ /*get some element in the array*/ /****************************************/ Locate(struct SqArray * A,va_list ap,int &off) { int i,ind; for (i =0;i<A->dim;++i) { ind = va_arg(ap,int); if (ind <0 || ind>=A->bounds[i]) return 0; off += A->constants[i]*ind; } return OK: } /****************************************/ /*get the value of some element in the array*/ /****************************************/ Value(struct * A,int &e) { int result; va_start(ap,e); if ((result = Locate(A,ap,off))<=0) return result; e = *(A->base + off); return 0; } /****************************************/ /*another way to get the element's value*/ /****************************************/ AssginArray(struct SqArray * A,int e,va_list ap) { va_start(ap,e); if ((result = Locate(A,ap,off))<=0) return result; *(A->base+off) =e; return OK: } main() { return 0; }
儘快給您回覆的。