Vectors in C : A Quick Guide to Creating Your Own Vectors in C
C has influenced most of the popular modern programming languages, like Perl, Java, Python and C++, to some degree or other. Because of this, many programmers consider that knowing C code makes it much easier to learn the new languages. However, C is a complete language in itself and it is still used everywhere, even more than four decades after it was created. C does have some advantages over its successors. It is a low level language, which means that it runs close to machine level languages. This makes it faster to compile and run than C++ or other languages. C compilers are also widely available, and can be used on almost any machine imagine.
Also, C can do almost everything that the other modern languages can, though it might take a while to accomplish. Vectors are a modern programming concept, which, unfortunately, aren’t built into the standard C library. They are found in C++, which is an object oriented programming extension of C. Essentially, vectors replace arrays in C++. In this tutorial, we’re going to give you an overview of how you can replicate vectors in C. This is an intermediate level tutorial. You need to be well versed with the basics of C to understand this tutorial. If you’re new to C programming, we recommend you first take this C beginners course to learn the basics.
Arrays vs. Vectors
Before we begin with how we can replicate vectors in C, let’s take a look at the major differences between arrays and vectors, and why exactly you might need vectors in C. Arrays are a special type of variable that can store multiple data values of the same type. In C, you have to declare an array with a specific size before you can use it. The array will then be able to store data, according to its size. You cannot store more data than the initial array size you specified. This means that your arrays have to be perfectly sized – you can’t increase the size later if you need to. You will have to get your research right before you create an array for a large, complex program. Additionally, arrays can only store primitive data types, like int or float.
A vector is a type of array you find in object oriented languages like C++. Like arrays, they can store multiple data values. However, unlike arrays, they cannot store primitive data types. They only store object references – they point to the objects that contain the data instead of storing the objects themselves. Also, you don’t have to declare the size of a vector. It will grow, or shrink, as you fill it with object references or remove them. Vectors also have several safety features that make them easier to use than arrays, and chances of your program crashing are much less. To learn more about vectors and arrays, you can take a look at our tutorials on the topic. You can also sign up for this C course– we cover data structure basics in greater detail in it. You can see why vectors sound more useful than arrays, and why they might be useful in C. However, C is not an object oriented language, so creating a true vector is virtually impossible. We can, however, create a pseudo vector in C in a couple of different ways.
Replicating a Vector in C
You can use a data structure to hold a vector. You will want to create your own data type (vector type) by using the typedef keyword:
typedef struct dynamic_vector { int* data; size_t limit; // Total size of the vector size_t current; //Number of vectors in it at present } vectorv;
Here, we’ve created our own structure that will mimic a vector. The typedef keyword lets you define a data type with a different name. In this case, we have used the name vector for the structure dynamic_vector. A structure is a data type in C that holds items with different values. It is also able to hold different data types, like int, char and float, without a problem. You can see why this would be perfect for us. The “struct” keyword initializes the structure and tells C it has three values: a pointer, the limit (or total size) of the vector and the current value (the total number of elements present in the vector at the moment).
After you have declared a structure, you will want to write functions that help the structure mimic the way a vector works. To really know how a vector works, you will need to study the various methods found in C++ that deal with vectors. You can take this course on C++ to learn more about them. One of the functions we need to write, for example, is making the structure to be able to increase or decrease its “limit” dynamically, so that when elements are added to it, it should expand automatically. For this, we would require the realloc functions. What does the realloc function do exactly? It changes the size of the memory block that was previously in use by the malloc function. So, in theory, we would now need a bigger size. The syntax for the realloc function is as follows:
void *realloc(void *current_ptr, size_t new_size)
Here, the current_ptr will be the current size, while the new_size parameter will let us specify the new size. You will want to make the new_size value double of the current_ptr value to prevent the need to call realloc all the time. This is just the beginning of the functions you need to write to make a vector. You will need to write functions to add elements to it, remove elements, delete the vector entirely or create new vectors. You will also need to take into account how the C memory management works to create a program that doesn’t keep crashing. Learn more about writing effective C programs with this course.
Recommended Articles
Top courses in C# (programming language)
C# (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.