Category : Files from Magazines
Archive   : CUJ9304.ZIP
Filename : 1104109B

 
Output of file : 1104109B contained in archive : CUJ9304.ZIP
/* sort4.c: Sort structures via pointers */

#include
#include
#include

#define NELEMS 4

struct person
{
char last[16];
char first[11];
char phone[13];
int age;
};

static int name_comp(const void *, const void *);
static int age_comp(const void *, const void *);

main()
{
size_t i;
static struct person some_people[NELEMS] =
{{"Lincoln","Abraham","555-1865",161},
{"Ford","Henry","555-1903",98},
{"Ford","Edsel","555-1965",53},
{"Trump","Donald","555-1988",49}};
struct person *some_ptrs[NELEMS] =
{some_people,some_people+1,some_people+2,some_people+3};

qsort(some_ptrs, NELEMS, sizeof some_ptrs[0], name_comp);

puts("By name:");
for (i = 0; i < NELEMS; ++i)
printf("%s, %s, %s %d\n",
some_ptrs[i]->last,
some_ptrs[i]->first,
some_ptrs[i]->phone,
some_ptrs[i]->age);

qsort(some_ptrs, NELEMS, sizeof some_ptrs[0], age_comp);

puts("\nBy age:");
for (i = 0; i < NELEMS; ++i)
printf("%s, %s, %s %d\n",
some_ptrs[i]->last,
some_ptrs[i]->first,
some_ptrs[i]->phone,
some_ptrs[i]->age);
return 0;
}

static int name_comp(const void *p1, const void *p2)
{
struct person *sp1 = * (struct person **) p1;
struct person *sp2 = * (struct person **) p2;
int order = strcmp(sp1->last,sp2->last);

if (order == 0)
order = strcmp(sp1->first,sp2->first);
return order;
}

static int age_comp(const void *p1, const void *p2)
{
struct person *sp1 = * (struct person **) p1;
struct person *sp2 = * (struct person **) p2;

return sp1->age - sp2->age;
}



  3 Responses to “Category : Files from Magazines
Archive   : CUJ9304.ZIP
Filename : 1104109B

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/