All pastes #943924 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-03-15 18:29 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    size_t length;
    size_t allocated;
    char first;
} string_t;

string_t *str(size_t sz)
{
    string_t *s = malloc(sizeof(string_t) + (sz ? sz-1 : 0));
    s->length = sz;
    s->allocated = sz;
    return s;
}

#define DATA(s) (&(s)->first)

void prin(string_t *foo)
{
    int i;
    for (i = 0; i < foo->length; i++) {
        putchar(DATA(foo)[i]);
    }
}

int main(void)
{
    string_t *foo = str(13);
    DATA(foo)[0] = 'H';
    DATA(foo)[1] = 'e';
    DATA(foo)[2] = 'l';
    DATA(foo)[3] = 'l';
    DATA(foo)[4] = 'o';
    DATA(foo)[5] = ',';
    DATA(foo)[6] = ' ';
    DATA(foo)[7] = 'w';
    DATA(foo)[8] = 'o';
    DATA(foo)[9] = 'r';
    DATA(foo)[10] = 'l';
    DATA(foo)[11] = 'd';
    DATA(foo)[12] = '!';
    prin(foo);
    return 0;
}