All pastes #416924 Raw Edit

Mine

public text v1 · immutable
#416924 ·published 2007-03-30 16:11 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static char *add_char_to_buffer(char c, char **buffer, int *size)
{
    int length = strlen(*buffer) + 1;
    
    if(length == *size)
    {
        *size *= 2;
        *buffer = realloc(*buffer, *size);
    }
    
    if(*buffer != NULL)
    {
        *buffer[length - 1] = c;
        *buffer[length] = '\0';
    }

    return *buffer;
}

int main()
{
    int size = 1;
    char *buffer = (char *) malloc(size * sizeof(char));
    buffer[0] == '\0';

    int i;

    for(i = 0; i < 10; i++)
    {
        add_char_to_buffer('a', &buffer, &size);
        printf("%s\n", buffer);
    }
}