Last active
September 20, 2017 13:24
-
-
Save Romain-P/c4517d41775d3fc482a075a839fe89fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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