### Implementing Vector - C Programming Tutorial

In this c programming tutorial we are going to see code for implementing vector in c language or creating array of size N (Any size array) in language. So let’s proceed, in this c programming tutorial we’re just going to create a dynamically sized array of integers. Here’s what the definition of a vector interface might look like:

// vector.h#define VECTOR_INITIAL_CAPACITY 100// Define a vector typetypedef struct { int size; // slots used so far int capacity; // total available slots int *data; // array of integers we're storing} Vector;void vector_init(Vector *vector);void vector_append(Vector *vector, int value);int vector_get(Vector *vector, int index);void vector_set(Vector *vector, int index, int value);void vector_double_capacity_if_full(Vector *vector);void vector_free(Vector *vector);

Note that we are writing this code in header file and we will be calling it vector.h

**Implementing Vector**

Below is the code for implementation of the interface we defined (vector.h) above. We will be naming this code as vector.c

// vector.c#include <stdio.h>#include <stdlib.h>#include "vector.h"void vector_init(Vector *vector) { // initialize size and capacity vector->size = 0; vector->capacity = VECTOR_INITIAL_CAPACITY; // allocate memory for vector->data vector->data = malloc(sizeof(int) * vector->capacity);}void vector_append(Vector *vector, int value) { // make sure there's room to expand into vector_double_capacity_if_full(vector); // append the value and increment vector->size vector->data[vector->size++] = value;}int vector_get(Vector *vector, int index) { if (index >= vector->size || index < 0) { printf("Index %d out of bounds for vector of size %d\n", index, vector->size); exit(1); } return vector->data[index];}void vector_set(Vector *vector, int index, int value) { // zero fill the vector up to the desired index while (index >= vector->size) { vector_append(vector, 0); } // set the value at the desired index vector->data[index] = value;}void vector_double_capacity_if_full(Vector *vector) { if (vector->size >= vector->capacity) { // double vector->capacity and resize the allocated memory accordingly vector->capacity *= 2; vector->data = realloc(vector->data, sizeof(int) * vector->capacity); }}void vector_free(Vector *vector) { free(vector->data);}

What we did here is simply allocated memory using malloc() function. The size of the memory allocated will be the simply the size of the vector we want, multiplied by the size of type of the vector we are going to create. Here we are creating Vector for storing integer values, so the size of memory would be “sizeof(int) * vector->capacity”. Note that if you want create vector for storing values with some other data type, then simply change the data type of the “* data” in structure we have defined to required one and that’s it.

**Using our Vector**

In this usage example, we keep things simple and just pass around the pointer to a variable called `vector`

allocated on the stack, we will be naming this file as vector-usage.c

// vector-usage.c#include <stdio.h>#include "vector.h"int main() { // declare and initialize a new vector Vector vector; vector_init(&vector); // fill it up with 150 arbitrary values // this should expand capacity up to 200 int i; for (i = 200; i > -50; i--) { vector_append(&vector, i); } // set a value at an arbitrary index // this will expand and zero-fill the vector to fit vector_set(&vector, 4452, 21312984); // print out an arbitrary value in the vector printf("Heres the value at 27: %d\n", vector_get(&vector, 27)); // we're all done playing with our vector, // so free its underlying data array vector_free(&vector);}

## Comments

## Post a Comment