Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the data structure of multidimensional array in C language

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "C language multidimensional array data structure how to achieve", in daily operation, I believe many people in C language multidimensional array data structure how to achieve the problem there are doubts, small series consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "C language multidimensional array data structure how to achieve" doubts helpful! Next, please follow the small series to learn together!

multidimensional array of data structures

defining structure

typedef struct { ElemType* base;//array element base (array base) int dim;//array dimension int* bounds;//array dimension bounds base (store each bit length information) int* constants;//array mapping function constants base address}Array; prototype description of each basic operation function

1) Creating an array

//If the function parameters are legal, construct the array AStatus InitArray(Array* A, int dim,...);

(2) Destroy the array

//DestroyArray Status DestroyArray(Array* A);

(3) Positioning of the array

//Status LocateArray(Array A, va_list ap, int* offset);

(4) Assignment of array elements

//A is an n-dimensional array, e is an element variable, followed by n subscript values//If the subscript is not out of bounds, then assign the value of e to the specified element of A (assignment) Status SetArray(Array* A, ElemType e,...);

(5) Get array elements

//A is an n-dimensional array, e is an element variable, followed by n subscript values//If the subscript is not out of bounds, then e is assigned to the specified element of A (get) Status GetValue(ElemType* e, Array A,...); Specific implementation of basic operations

(1) Create an array function implementation

//Create multidimensional array Status InitArray(Array* A, int dim,...) { if (dim MAX_ARRAY_DIM) return ERROR;//Invalid argument A->dim = dim; A->bounds = (int*)malloc(sizeof(int) * dim); if (! A->bounds) return OVERFLOW;//memory allocation failed //If the length of each dimension is legal, store it in A.bounds and find the total number of elements of A elemtotal int elemtotal = 1; va_list ap; va_start(ap, dim); for (int i = 0; i

< dim; ++i) { A->

bounds[i] = va_arg(ap, int); if (A->bounds[i]

< 0)return UNDERFLOW; elemtotal *= A->

bounds[i]; } va_end(ap); //allocate memory space for array A->base = (ElemType*)malloc(sizeof(ElemType) * elemtotal); if (! A->base) return OVERFLOW;//memory allocation failed //Find mapping function Ci and store A.constants[i-1],i = 1,..., dim; A->constants = (int*)malloc(sizeof(int) * dim); if (! A->constants) return OVERFLOW;//memory allocation failed A->constants[dim - 1] = 1; for (int i = dim - 2; i >= 0; --i) { A->constants[i] = A->bounds[i + 1] * A->constants[i + 1]; } return OK;}

(2) Destroy array function implementation

//DestroyArray Status DestroyArray(Array* A) { if (! A->base) return ERROR; free(A->base); A->base = NULL; if (! A->bounds) return ERROR; free(A->bounds); A->bounds = NULL; if (! A->constants) return ERROR; free(A->constants); A->constants = NULL; return OK;}

(3) Array positioning function implementation

//array locationStatus LocateArray(Array A, va_list ap, int* offset) { int i, instand; //If the element index indicated by ap is reasonable, find the relative position of the element and return to offset *offset = 0; for (i = 0; i

< A.dim; i++) { instand = va_arg(ap, int); if (instand < 0 || instand >

A.bounds[i]) { // printf("instand = %d, failed\n",instand);//debug code return ERROR; } *offset += A.constants[i] * instand; } return OK;}

(4) Array element assignment function implementation

//Array assignment Status SetArray(Array *A, ElemType e,...) { va_list ap; int offset; va_start(ap, e); if (LocateArray(*A, ap, &offset) == ERROR) return ERROR; va_end(ap); *(A->base + offset) = e; return OK;}

(5) Take out array element function implementation

//Gets the value of an array element and returns Status GetValue(ElemType* e, Array A,...) with E { va_list ap; int offset; va_start(ap, A); if (LocateArray(A, ap, &offset) == ERROR) return ERROR; va_end(ap); *e = *(A.base + offset); return OK;} Test Analysis

create

Create a two-dimensional array with a length of 4 in the first dimension and 3 in the second dimension.

Test Code:

Run Results:

destruction

Pass the address of struct A into DestroyArray and perform the operation.

Test Code:

Run Results:

array element assignment

Define two-dimensional array B[4][3], assign its value to array A through SetArray function, and determine whether the assignment is accurate by traversing the values of the elements in output A.

Test Code:

Run Results:

fetching array elements

Test Code:

Run Results:

Thinking and summary 1. Re-understanding of arrays

The memory structure is one-dimensional linear structure, array is multidimensional structure. If you want to put a multidimensional structure in a one-dimensional storage unit, you must first convert the multidimensional array into a one-dimensional linear sequence before you can put it in memory. There are two main ways to store arrays: one is to store them in row order, and the other is to store them in column order.

2. Problems encountered during debugging and solutions

1, two compilation errors

va_start argument must not have reference type and must not be parenthesized;

va_start function usage problem, function prototype: void va_start (va_list ap, parmN); error reason is incorrect parameters. Check out the c language development manual for reasons.

ap An instance of type va_list

Prmhn Named parameter before the first variable parameter

② Error message: *LNK2019 cannot resolve the external symbol "int _cdecl SetArray(struct Array ,int,int,…)" (? SetArray@@YAHPAUArray@@HHZZ), referenced in function_main

This error message is, find the definition but did not find the implementation of the function, so you need to implement the function before calling, while paying attention to the corresponding parameters to avoid the above problems.

2. Operation error

Run error, data access problem. By checking the statements before and after the error message, it is found that forgetting i+1 when accessing the array causes i to go to-1 to form an error.

3. Error in operation result

The operation result shows that both address and value are output. Through debugging, it is found that after entering LocateArray function for the first time, the function returns ERROR. Through printing statement inspection, the function does enter judgment statement and returns ERROR;

It indicates that the parameter is inaccurate or the function judgment statement is incorrect. Because the value is controlled by oneself, the possibility of inaccurate parameter is small. The critical parameter and function logic are analyzed carefully, and the condition of judging parameter is changed into correct judgment statement. Get the right results.

3. Time complexity analysis of the algorithm

Time Complexity: O(n)

Time Complexity: O(1)

Time Complexity: O(n)

Time Complexity of the SetArray function is O(n);

Time complexity of the function is O(n).

The time complexity of the SetArray and GetArray functions is primarily affected by the LocateArray functions.

At this point, on the "C language multidimensional array data structure how to achieve" the study is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report