All pastes #1180886 Raw Edit

Miscellany

public c v1 · immutable
#1180886 ·published 2008-08-21 17:51 UTC
rendered paste body
#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>char look;#define CHUNK_SIZE 16#define read_char() do { look = getc(stdin); } while (0)int get_name(char **name){    int i = 0;    int chunk_size = CHUNK_SIZE;    char *buffer = calloc(chunk_size, sizeof(char));    while (!isspace(look))    {        buffer[i++] = look;        read_char();        if (i == chunk_size)        {            chunk_size += CHUNK_SIZE;            buffer = realloc(buffer, chunk_size * sizeof(char));        }    }    *name = calloc(i, sizeof(char));    memmove(*name, buffer, i * sizeof(char));    free(buffer);    return i;}void init(){    read_char();}int main(){    init();    char* name;    get_name(&name);    printf("Name: %s\n", name);    free(name);    return 0;}