_ DataStructure_C_Impl: Array,
# Include <stdio. h> # include <stdlib. h> # include <stdarg. h> # define MaxArraySize 2 typedef int DataType; typedef struct {DataType * base; // The base address of the array element int dim; // The array dimension int * bounds; // The boundary between each dimension of the Array int * constants; // The base address of the Array storage image constant} Array; // The initialization operation of the Array int InitArray (Array *, int dim ,...) {int elemtotal = 1, I; // elemtotal is the total number of array elements. The initial value is 1va_list ap; // declare va_listif (dim <1 | dim> MaxArraySize) // If the dimension is invalid, return 0 return 0; A-> dim = dim; A-> bounds = (int *) Malloc (dim * sizeof (int); // allocate a dim memory unit if (! A-> bounds) exit (-1); va_start (ap, dim); // dim is A fixed parameter, that is, the previous parameter of A Variable Parameter for (I = 0; I <dim; ++ I) {A-> bounds [I] = va_arg (ap, int); // obtain variable parameters in sequence, that is, the length of each dimension, if (A-> bounds [I] <0) return-1; elemtotal * = A-> bounds [I]; // obtain the total number of elements in the array} va_end (ap); A-> base = (DataType *) malloc (elemtotal * sizeof (DataType )); // allocate memory space for all elements in the array if (! A-> base) exit (-1); A-> constants = (int *) malloc (dim * sizeof (int )); // allocate the memory unit if (! A-> constants) exit (-1); A-> constants [dim-1] = 1; for (I = dim-2; I> = 0; -- I) a-> constants [I] = A-> bounds [I + 1] * A-> constants [I + 1]; return 1 ;}// destroy the array. Release the dynamically applied memory unit void DestroyArray (Array * A) {if (A-> base) free (A-> base); if (A-> bounds) free (A-> bounds); if (A-> constants) free (A-> constants); A-> base = A-> bounds = A-> constants = NULL; // point each pointer to null A-> dim = 0;} // obtain the relative address offsetint LocateArray (Array, va_list ap, int * offset) {int I, instand; * offset = 0; for (I = 0; I <. dim; I ++) {instand = va_arg (ap, int); if (instand <0 | instand> =. bounds [I]) return 0; * offset + =. constants [I] * Instand;} return 1;} // return the specified Element in the Array. Assign the element of the specified Array to eint GetValue (DataType * e, Array ,...) {va_list ap; int offset; va_start (ap, A); if (LocateArray (A, ap, & offset) = 0) // find the relative position of the element in the array. return 0; va_end (ap); * e = * (. base + offset); // assign the element value to ereturn 1;} // assign the value to the array. Int AssignValue (Array A, DataType e ,...) {va_list ap; int offset; va_start (ap, e); if (LocateArray (A, ap, & offset) = 0) // find the relative position of the element in the array. return 0; va_end (ap); * (. base + offset) = e; // assign e to return 1;} void main () {Array A; DataType B [4] [3] = {5, 6, 7 },{, 67 },{ 35, 2, 34 },{, 90 }}; int I, j; int dim = 2, bound1 = 4, bound2 = 3; /* initialize the dimension of the array and the length of each dimension */DataType e; InitArray (& A, dim, bound1, bound2 ); /* construct A 4 × 3 two-dimensional array A */printf ("the length of each dimension of array A is:"); for (I = 0; I <dim; I ++)/* output the length of each dimension of array A */printf ("% 3d",. bounds [I]); printf ("\ n Array A's constant base address is:"); for (I = 0; I <dim; I ++) /* output the constant base address of array A */printf ("% 3d",. constants [I]); printf ("\ n % d row % d matrix element: \ n", bound1, bound2); for (I = 0; I <bound1; I ++) {for (j = 0; j <bound2; j ++) {AssignValue (A, B [I] [j], I, j);/* assign the element of array B to A */GetValue (& e, A, I, j ); /* assign the elements in array A to e */printf ("A [% d] [% d] = % 3d \ t", I, j, e ); /* output element */} printf ("\ n") in array A;} printf ("outputs elements according to the linear sequence of the array, that is, outputs elements using the base address: \ n "); for (I = 0; I <bound1 * bound2; I ++) /* output the element */{printf ("element % d = % 3d \ t", I + 1,. base [I]); if (I + 1) % bound2 = 0) printf ("\ n");} DestroyArray (& ); system ("pause ");}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.