Skip to content

Instantly share code, notes, and snippets.

@robinedwards
Created March 4, 2012 16:44
Show Gist options
  • Save robinedwards/1973821 to your computer and use it in GitHub Desktop.
Save robinedwards/1973821 to your computer and use it in GitHub Desktop.
Re-learning C, very much enjoying it with OO style.
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *name;
int age;
int height;
int weight;
} Person;
Person *Person_create(char *name, int age, int height, int weight) {
assert(name !=NULL);
Person *who = malloc(sizeof(Person));
assert(who != NULL);
who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;
return who;
}
void Person_destroy(Person *who) {
assert(who != NULL);
free(who->name);
free(who);
}
void Person_print(Person *who) {
assert (who != NULL);
printf("Name: %s, Age: %d, Height: %d Weight: %d",
who->name,
who->age,
who->height,
who->weight
);
}
int main(int argc, char *argv[]) {
Person *jim;
jim = Person_create(
"Jim",
32,
6,
11
);
Person_print(jim);
Person_destroy(jim);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment