// C language struct person { // use struct to record data char *name; // name is really a string, awkward in C int age; }; typedef struct person *person_ptr; // C is famous for ptrs to things // function acts as a constructor of new person records person_ptr personConstructor(char *name, int age) { // new name and age person_ptr p; // declare a ptr p = (person_ptr)malloc(sizeof(struct person)); // allocate space from dynamic memory p->name = name; // fill-in the fields p->age = age; return p; // return ptr to caller } void personDestructor(person_ptr p) { // return space back to dynamic memory free(p); } char *getName(person_ptr p) { // accessor function to return name return p->name; // NOTE: ptr is parameter to function } // Opposite in OO: obj.method() int getAge(person_ptr p) { // accessor function return p->age; } char* toString(person_ptr p) { // covert fields into one string char *buffer = (char *)malloc(sizeof(char)*50); // memory leak - buffer never free'd sprintf(buffer,"%s %d",getName(p),getAge(p)); return buffer; }