Create a dynamic array in C Language

Source: Internet
Author: User

Create a dynamic array in C Language

The array is a bit of random access, but its deficiency is also obvious, that is, once it is created, its size cannot be changed. If you use arrays to store data, you must create an array that may store the largest space, which is a waste of space. Dynamic Array solves this problem. The idea of a dynamic array is: first create an array of a certain size to store data in this array. If the array is full, apply for a larger space for storage. You can specify the increment (inc) size or fixed the size each time you apply for a new one. The advantage of doing so is that there is not much waste of space, and a maximum of (inc-1) element space is wasted. The disadvantage is that it is a waste of time to apply for a new space, each time you apply for a new space, you must copy the original data to the new space. When the array is large, this waste is quite impressive. The linked list and array will be used later to solve this problem.

First, create a storage structure for Dynamic Arrays:
Typedef unsigned char BOOL;
Typedef int elem_t; // data storage type
Typedef struct
{
Int iCount; // The number of data elements in the array)
Int iCapacity; // capacity (maximum number of elements in the array)
Elem_t * pdata; // Data Pointer (this pointer points to the first address of the data storage space)
} Array_t;

The basic operations for defining arrays are as follows:

1. Initialization;
2. Set the element value;
3. Obtain the element reference (address in C );
4. Obtain the element value;
5. Destroy Arrays

The function declaration for the preceding five operations is as follows:
Bool initarray (array_t * array, int size); // initialization, size indicates the size of the specified initialization Array
Bool setvalue (array_t * array, int index, elem_t Val); // you can specify the value of the element at the specified position.
Elem_t * getref (array_t * array, int index); // obtain the reference (address) of the element at the specified position)
Elem_t getValue (Array_t * array, int index); // obtain the value of the element at the specified position.
BOOL destroyArray (Array_t * array); // destroy the array

The following is the function implementation:
# Define INIT_DATA_NUM 10 // array initialization size, incremental size

Bool initarray (array_t * array, int size) // initialization. If the size is <= 0, the default size is used.
{
Bool Bret = false;
Int initsize = (size> 0 )? Size: init_data_num;

Array-> pdata = (elem_t *) malloc (initsize * sizeof (elem_t ));
If (array-> pdata! = NULL)
{
Array-> icapacity = initsize;
Array-> icount = 0;
Bret = true;
}
Return Bret;
}

Bool setvalue (array_t * array, int index, elem_t Val) // you can specify the value of an element in a specified position.
{
Bool Bret = false;
If (index> 0 & index <array-> icount)
{
Array-> pdata [Index] = val;
Bret = true;
}
Return Bret;
}
Elem_t * getref (array_t * array, int index) // get the reference of the element at the specified position (address)
{
Elem_t * eret = NULL;
If (index> 0 & index <array-> icount)
{
ERet = array-> pData + index;
}
Return eRet;
}

Elem_t getValue (Array_t * array, int index) // obtain the value of the element at the specified position (the array out-of-bounds is not checked)
{
Return array-> pData [index];
}

BOOL destroyArray (Array_t * array) // destroy the array
{
Free (array-> pData );
Array-> pData = NULL;
Return TRUE;
}

This completes the basic operations on dynamic arrays. The following describes some advanced operations on dynamic arrays, such as traversing, searching, sorting, and copying.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.