How can we use vector in C?

Here is source code of the C Program to Implement Vector. 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. #include <stdlib.h>
    0
  12. #include <stdlib.h>
    1
  13. #include <stdlib.h>
    2
  14. #include <stdlib.h>
    3
  15. #include <stdlib.h>
    4
  16.  
  17. #include <stdlib.h>
    6
  18. #include <stdlib.h>
    7
  19. #include <stdlib.h>
    8
  20. #include <stdlib.h>
    9
  21.  
    0
  22.  
  23.  
    2
  24.  
    3
  25.  
    4
  26.  
    5
  27.  
    6
  28.  
    7
  29.  
    8
  30.  
    9
  31.  
  32. #ifndef VECTOR_H
    1
  33.  
  34. #ifndef VECTOR_H
    3
  35. #ifndef VECTOR_H
    4
  36. #ifndef VECTOR_H
    5
  37. #ifndef VECTOR_H
    6
  38. #ifndef VECTOR_H
    7
  39.  
  40. #ifndef VECTOR_H
    9
  41. #define VECTOR_H
    0
  42. #ifndef VECTOR_H
    7
  43.  
  44. #define VECTOR_H
    3
  45. #define VECTOR_H
    4
  46. #define VECTOR_H
    5
  47. #ifndef VECTOR_H
    1
  48.  
  49. #define VECTOR_H
    8
  50. #define VECTOR_H
    9
  51.  
    0
  52.  
    1
  53.  
    2
  54. #ifndef VECTOR_H
    7
  55.  
  56.  
    5
  57.  
    6
  58.  
    7
  59.  
    8
  60. #ifndef VECTOR_H
    7
  61.  
  62. #define VECTOR_INIT_CAPACITY 4
    1
  63. #define VECTOR_INIT_CAPACITY 4
    2
  64. #define VECTOR_INIT_CAPACITY 4
    3
  65. #ifndef VECTOR_H
    7
  66.  
  67. #define VECTOR_INIT_CAPACITY 4
    6
  68. #define VECTOR_INIT_CAPACITY 4
    2
  69. #define VECTOR_INIT_CAPACITY 4
    8
  70. #define VECTOR_INIT_CAPACITY 4
    9
  71. #ifndef VECTOR_H
    7
  72.  
  73.  
    2
  74.  
    3
  75.  
    4
  76.  
  77.  
    6
  78.  
    7
  79.  
    8
  80.  
    9
  81. #define VECTOR_INIT(vec) vector vec; vector_init(&vec)
    0
  82.  
    2
  83.  
  84. #define VECTOR_INIT(vec) vector vec; vector_init(&vec)
    3
  85.  
  86. #define VECTOR_INIT(vec) vector vec; vector_init(&vec)
    5
  87. #define VECTOR_INIT(vec) vector vec; vector_init(&vec)
    6
  88. #ifndef VECTOR_H
    7
  89.  
  90. #define VECTOR_INIT(vec) vector vec; vector_init(&vec)
    9
  91. #define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
    0
  92. #ifndef VECTOR_H
    7
  93.  
  94. #define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
    3
  95.  
    7
  96.  
  97. #define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
    6
  98. #define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
    7
  99.  
  100. #define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
    9
  101. #include <stdlib.h>
    00
  102. #include <stdlib.h>
    01
  103. #include <stdlib.h>
    02
  104.  
  105. #include <stdlib.h>
    04
  106. #include <stdlib.h>
    05
  107. #include <stdlib.h>
    06
  108.  
  109. #include <stdlib.h>
    08
  110. #include <stdlib.h>
    09
  111. #include <stdlib.h>
    10
  112.  
  113. #include <stdlib.h>
    12
  114. #include <stdlib.h>
    13
  115.  
  116. #include <stdlib.h>
    04
  117. #include <stdlib.h>
    05
  118. #include <stdlib.h>
    06
  119.  
  120. #include <stdlib.h>
    19
  121. #ifndef VECTOR_H
    7

Output:

#include <stdlib.h>
21

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

advertisement

advertisement

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

« Prev - C Program to Implement Skip List

» Next - C Program to Implement Hash Tables Chaining with Linked Lists

Next Steps:

  • Get Free Certificate of Merit in Data Structure I
  • Participate in Data Structure I Certification Contest
  • Become a Top Ranker in Data Structure I
  • Take Data Structure I Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Related Posts:

  • Apply for Data Structure Internship
  • Buy Data Structure Books
  • Practice Programming MCQs
  • Buy Programming Books
  • Apply for Computer Science Internship

Can we use vector in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

Why do we use vector in C?

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically. That is, we can change the size of the vector during the execution of a program as per our requirements. Vectors are part of the C++ Standard Template Library.

What can I use instead of vector in C?

Alternatives to Vector in C++.
array..
Deque..
Linked List..
multiset..

What is the use of vector in programming?

Vectors are used in many programming languages as data structure containers. They have a dynamic structure and provide programmers with the ability to allocate container size and memory space quickly. In this sense, vectors can be thought of as dynamic arrays.