Growing String in c
This article contains the source code for a c utility that manages a growing string, and a brief explanation of the code.
To obtain this, we make something similar to a c++ class: a struct with associated functions. The functions add characters to the string in the struct, and allocate more memory space when the string is full.
This is usefull when we are working with string with an unknow length (e.g. we are reading a file).
The struct containing the string is the following:
// Struct containing the growing string
typedef struct
{
// string
char * text;
// string length (with zero terminator)
int length;
// string capacity
int capacity;
} growing_string;
The text variable is where the string is allocated, length is the current string lenght and capacity is the memory allocated for the string.
To better understand the difference between length and capacity, look at the next image:

Capacity is the total space allocated for the string, while length is the actual number of character of the string, including the \0 terminator (the special character that close the string). If we add enough characters and the length is greater than the capacity, we need to allocate more memory for the string.