Skip to content

Instantly share code, notes, and snippets.

@abiriadev
Last active November 26, 2023 03:54
Show Gist options
  • Save abiriadev/30fb580247cf0d7d8710a96541906803 to your computer and use it in GitHub Desktop.
Save abiriadev/30fb580247cf0d7d8710a96541906803 to your computer and use it in GitHub Desktop.
C's subtyping demonstration example
#include <stdio.h>
struct Person {
int age;
};
struct Student {
int age;
int score;
};
void print_age(void *p) {
struct Person person = *((struct Person *)p);
printf("my age: %d\n", person.age);
}
void print_score(void *p) {
struct Student student = *((struct Student *)p);
printf("my score: %d\n", student.score);
}
int main() {
struct Person p1 = {44};
struct Student s1 = {16, 90};
print_age(&p1);
print_age(&s1);
print_score(&p1);
print_score(&s1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment