Last active
December 13, 2016 22:49
-
-
Save cbergau/5ab35e097359d089cad0768ab1572fbd to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <windows.h> | |
#include <stdio.h> | |
typedef struct person { | |
char *firstName; | |
char *lastName; | |
int yearOfBirthday; | |
char *job; | |
} PERSON; | |
PERSON *make_person(const char *firstName, const char *lastName, const int yearOfBirth, const char *job) { | |
PERSON *person = (PERSON *) malloc(sizeof(PERSON)); | |
person->firstName = (char *) firstName; | |
person->lastName = (char *) lastName; | |
person->job = (char *) job; | |
person->yearOfBirthday = yearOfBirth; | |
return person; | |
} | |
void free_person(PERSON *person) { | |
free(person); | |
} | |
void print_person(PERSON *person) { | |
printf("FirstName: %s, LastName: %s, Job: %s, Year of Birth: %d\n", | |
person->firstName, | |
person->lastName, | |
person->job, | |
person->yearOfBirthday | |
); | |
} | |
int cmpfunc(const void *a, const void *b) { | |
return (*(int *) a - *(int *) b); | |
} | |
int cstring_cmp(const void *a, const void *b) { | |
const char **ia = (const char **) a; | |
const char **ib = (const char **) b; | |
return strcmp(*ia, *ib); | |
} | |
int main() { | |
int values[] = {22, 23, 454, 1, 2, 3}; | |
qsort(values, 6, sizeof(int), cmpfunc); | |
char *strings[] = {"Zorro", "Alex", "Celine", "Bill", "Forest", "Dexter"}; | |
qsort(strings, sizeof(strings) / sizeof(char *), sizeof(char *), cstring_cmp); | |
PERSON *christian = make_person("Christian", "Bergau", 1986, "Software Architect"); | |
PERSON *christina = make_person("Christina", "WeissIchNicht", 1989, "Studentin"); | |
print_person(christian); | |
print_person(christina); | |
free_person(christian); | |
free_person(christina); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment