Define Max_array_dim 8//assumes that the maximum value of the array dimension is 8
typedef int ELEMTYPE;
struct Array
{
Elemtype * base;//array element base address, assigned by Initarray
int dim;//Array Dimension
int * bounds;//array dimension base address, assigned by Initarray
int * constants;//array image function constant base address, assigned by Initarray
};
Status Initarray (Array &a, int Dim, ...) {//Yanet number Dim and the length of each dimension is valid, construct the corresponding array A and return OK
int elemtotal = 1, i;//elemtotal is the total number of array elements, the initial value is 1 (multiplier)
Va_list ap;//variable length parameter table type, in Stdarg.h
if (Dim < 1 | | Dim > Max_array_dim)//array dimension out of range
return ERROR;
A.dim = number of dim;//array dimensions
A.bounds = (int) malloc (Dimsizeof (int));//dynamic allocation of array dimension boundary addresses
if (! A.bounds)
Exit (OVERFLOW);
Va_start (AP, dim);//variable-length parameter "..." starts after the formal parameter dim
for (i = 0; i < Dim; i++)
{
A.bounds[i] = Va_arg (AP, int); Assign the variable length parameter to A.bounds[i]
if (A.bounds < 0)
Return underflow;//is defined in MATH.H as 4
Elemtotal *= a.bounds[i];//Total array elements = product of each dimension length
}
Va_end (AP);//End extracting variable-length parameters
A.base = (elemtype) malloc (elemtotalsizeof (Elemtype));//dynamic allocation of array storage space
if (! A.base)
Exit (OVERFLOW);
a.constants = (int) malloc (Dimsizeof (int));//dynamic allocation array offset base Address
if (! a.constants)
Exit (OVERFLOW);
A.CONSTANTS[DIM-1] = 1;//The offset of the last dimension is 1
for (i = dim-2; I >= 0;–i)
A.constants[i] = a.bounds[i + 1] * a.constants[i-1];//offset per dimension
return OK;
}
void Destroyarray (array &a) {//Destroy array A
if (a.base)
Free (a.base);//release the storage unit that the a.base points to
if (a.bounds)
Free (a.bounds);
if (a.constants)
Free (a.constants);
A.base = A.bounds = A.constants = null;//so they no longer point to any storage unit
A.dim = 0;
}
Status Locate (Array A, va_list ap, int &off) {//if each subscript value indicated by the AP is valid, then the relative address of the element in A is calculated off
int I, IND;
Off = 0;
for (i = 0; i < A.dim; i++)
{
IND = Va_arg (AP, int);//Read the subscript value of each dimension individually
if (Ind < 0 | | ind >= A.BOUNDS[I])//The subscript value of each dimension is not valid
return OVERFLOW;
Off + = a.constants[i] * ind;//relative address = subscript value of each dimension * the sum of the offsets of this dimension,
}
}
Status Value (elemtype &e, Array A, ...) {//"..." is the subscript value of each dimension in turn, and if the subscript is valid, E is assigned the corresponding element value of a.
Va_list ap;//variable length parameter table type, in Stdarg.h
int off;
Va_start (AP, A);//variable-length parameter "..." starts after formal parameter A
if (Locate (A, AP, off) = = OVERFLOW)//Call Locate () to obtain the relative address off of the unit referred to by the variable-length parameter
return ERROR;
E = * (a.base + off);//assigns the value of the cell to the variable-length parameter to E
return OK;
}
Status Assign (Array A, Elemtype e, ...) The value of {//variable A does not change, so & is not required, and "..." is the subscript value of each dimension, and if each subscript is valid, assigns the value of E to the specified element of a
va_list ap;int off;va_start(ap, e);if (Locate(A, ap, off) == OVERFLOW) return ERROR;*(A.base + off) = e;//将e的值赋给变长参数所指单元return OK;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sequential representation and implementation of arrays