Skip to content

Instantly share code, notes, and snippets.

@cbergau
Last active December 13, 2016 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbergau/5ab35e097359d089cad0768ab1572fbd to your computer and use it in GitHub Desktop.
Save cbergau/5ab35e097359d089cad0768ab1572fbd to your computer and use it in GitHub Desktop.
#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