VECTOR CODE EXAMPLE HOW TO
You can also see this article “ How to avoid dangling pointer“. You should remember that the address of the data should be valid and you have to care about the dangling pointer. I have also set the string using the index. After creation the vector I am displaying the stored string with there index. In this example, I am creating a vector of string using the pushback function. This function deallocates the allocated memory. VectorResize(v, v->vectorList.capacity / 2) If ((v->vectorList.total > 0) & ((v->vectorList.total) = (v->vectorList.capacity / 4))) int vectorDelete(vector *v, int index)įor (i = index (i vectorList.total - 1) ++i) This function assigns the NULL to the given index and shifts all elements in vector by 1. You need to typecast the memory as per the data types. If the index is not valid then it will return NULL ( null pointer). If the index is valid then this function will return the address of the data of the given index. Get the address of the data from the given index: If ((index >= 0) & (index vectorList.total)) int vectorSet(vector *v, int index, void *item) If you will pass the invalid index then it will do nothing. This function sets the data at the given index if the index is valid. Status = vectorResize(v, v->vectorList.capacity * 2) If (v->vectorList.capacity = v->vectorList.total) Int vectorPushBack(vector *v, void *item) If sufficient memory is not available then it will resize the memory. This function inserts the data at the end of the vector. Void **items = realloc(v->ems, sizeof(void *) * capacity) int vectorResize(vector *v, int capacity) You can also see the article, How to allocate dynamic memory in C. This function allocates the memory with a new size using realloc library function and updates the tracking parameter with the new value.
![vector code example vector code example](https://statisticsglobe.com/wp-content/uploads/2021/01/table-1-matrix-convert-matrix-to-list-of-column-vectors-in-r.png)
V->ems = malloc(sizeof(void *) * v->vectorList.capacity) V->vectorList.capacity = VECTOR_INIT_CAPACITY initialize the capacity and allocate the memory In this function, I am initializing the capacity of the vector and function pointers with the appropriate function. You can change the macro value as per your requirement. Now you need to create some macro for better readability and allocate the memory initially. Int (*pfVectorSet)(vector *, int, void *)
![vector code example vector code example](https://www.codegrepper.com/codeimages/how-to-find-length-of-vector-array-in-c--.png)
I am also creating here another structure to store the function pointer which points to the vector function (because C does not support the function in a structure like C++). Code implementation of the vector in Cįirst, we need to create a structure that stores the data and also track the stored data. The advantage of the void pointer is that we can store the address of any data type. This vector will store the address of the element using the void * ( Generic pointer, you can see this article). Because C is not supported to the template and vector so here I am creating a vector using the structure and pointer. So let’s come on the topic and create a simple vector.