C Program to Implement Variable Length Array

This is a C Program to implement variable length array(Vectors). An array (vector) is a common-place data type, used to hold and describe a collection of elements. These elements can be fetched at runtime by one or more indices (identifying keys). A distinguishing feature of an array compared to a list is that they allow for constant-time random access lookup, compared to the latters sequential access. Resizable arrays allow for an unspecified upper-bound of collection elements at runtime, and are conceptuality similar to a list. These dynamic arrays are more complicated and less used in introduction to its compatriot list, which is dynamic by nature. Using C as the language of implementation this post will guide you through building a simple vector data-structure. The structure will take advantage of a fixed-size array, with a counter invariant that keeps track of how many elements are currently present. If the underlying array becomes exhausted, the addition operation will re-allocate the contents to a larger size, by way of a copy.

Here is source code of the C Program to Implement Variable Length Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef VECTOR_H
  5. #define VECTOR_H
  6.  
  7. #define VECTOR_INIT_CAPACITY 4
  8.  
  9. #define VECTOR_INIT(vec) vector vec; vector_init(&vec)
  10. #define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
  11. #define VECTOR_SET(vec, id, item) vector_set(&vec, id, (void *) item)
  12. #define VECTOR_GET(vec, type, id) (type) vector_get(&vec, id)
  13. #define VECTOR_DELETE(vec, id) vector_delete(&vec, id)
  14. #define VECTOR_TOTAL(vec) vector_total(&vec)
  15. #define VECTOR_FREE(vec) vector_free(&vec)
  16.  
  17. typedef struct vector {
  18.     void **items;
  19.     int capacity;
  20.     int total;
  21. } vector;
  22.  
  23. void vector_init(vector *);
  24. int vector_total(vector *);
  25. static void vector_resize(vector *, int);
  26. void vector_add(vector *, void *);
  27. void vector_set(vector *, int, void *);
  28. void *vector_get(vector *, int);
  29. void vector_delete(vector *, int);
  30. void vector_free(vector *);
  31.  
  32. #endif
  33.  
  34. void vector_init(vector *v) {
  35.     v->capacity = VECTOR_INIT_CAPACITY;
  36.     v->total = 0;
  37.     v->items = malloc(sizeof(void *) * v->capacity);
  38. }
  39.  
  40. int vector_total(vector *v) {
  41.     return v->total;
  42. }
  43.  
  44. static void vector_resize(vector *v, int capacity) {
  45. #ifdef DEBUG_ON
  46.     printf("vector_resize: %d to %d\n", v->capacity, capacity);
  47. #endif
  48.  
  49.     void **items = realloc(v->items, sizeof(void *) * capacity);
  50.     if (items) {
  51.         v->items = items;
  52.         v->capacity = capacity;
  53.     }
  54. }
  55.  
  56. void vector_add(vector *v, void *item) {
  57.     if (v->capacity == v->total)
  58.         vector_resize(v, v->capacity * 2);
  59.     v->items[v->total++] = item;
  60. }
  61.  
  62. void vector_set(vector *v, int index, void *item) {
  63.     if (index >= 0 && index < v->total)
  64.         v->items[index] = item;
  65. }
  66.  
  67. void *vector_get(vector *v, int index) {
  68.     if (index >= 0 && index < v->total)
  69.         return v->items[index];
  70.     return NULL;
  71. }
  72.  
  73. void vector_delete(vector *v, int index) {
  74.     if (index < 0 || index >= v->total)
  75.         return;
  76.  
  77.     v->items[index] = NULL;
  78.     int i;
  79.     for (i = 0; i < v->total - 1; i++) {
  80.         v->items[i] = v->items[i + 1];
  81.         v->items[i + 1] = NULL;
  82.     }
  83.  
  84.     v->total--;
  85.  
  86.     if (v->total > 0 && v->total == v->capacity / 4)
  87.         vector_resize(v, v->capacity / 2);
  88. }
  89.  
  90. void vector_free(vector *v) {
  91.     free(v->items);
  92. }
  93.  
  94. int main(void) {
  95.     int i;
  96.  
  97.     vector v;
  98.     vector_init(&v);
  99.  
  100.     vector_add(&v, "Bonjour");
  101.     vector_add(&v, "tout");
  102.     vector_add(&v, "le");
  103.     vector_add(&v, "monde");
  104.  
  105.     for (i = 0; i < vector_total(&v); i++)
  106.         printf("%s ", (char *) vector_get(&v, i));
  107.     printf("\n");
  108.  
  109.     vector_delete(&v, 3);
  110.     vector_delete(&v, 2);
  111.     vector_delete(&v, 1);
  112.  
  113.     vector_set(&v, 0, "Hello");
  114.     vector_add(&v, "World");
  115.  
  116.     for (i = 0; i < vector_total(&v); i++)
  117.         printf("%s ", (char *) vector_get(&v, i));
  118.     printf("\n");
  119.  
  120.     vector_free(&v);
  121. }

Output:

$ gcc VariableLengthArray.c
$ ./a.out
 
Bonjour tout le monde 
Hello World

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.