Skip to content

Instantly share code, notes, and snippets.

@Romain-P
Last active September 20, 2017 13:24
Show Gist options
  • Select an option

  • Save Romain-P/c4517d41775d3fc482a075a839fe89fa to your computer and use it in GitHub Desktop.

Select an option

Save Romain-P/c4517d41775d3fc482a075a839fe89fa to your computer and use it in GitHub Desktop.
#include "string.h"
void main(int ac, char **args) {
String hello = string_init("hello");
hello->append(hello, " my girl");
printf("got a length of %d now", hello->length(hello));
}
#include "string.h"
static void length(String sef);
static void append(String self, String to_happend);
String string_init(char *str) {
String s = malloc(sizeof(*String));
s->length = &length;
s->append = &append;
}
static void length(String self) {
return (strlen(self->str));
}
static void append(String self, char *str) {
free(self->str);
self->str = strcat(self->str, str);
}
typedef struct s_string
{
char *str;
void (*length)(s_string *self);
void (*append)(s_string *self, char *str);
} *String;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment