voidptrdance

Example code for a blog post on generic programming in C
git clone https://git.tronto.net/voidptrdance
Download | Log | Files | Refs | README

main2.c (557B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include "PriorityQueue.h"
      4 
      5 int count_stars(void *v) {
      6 	char *s = (char *)v;
      7 	int i = 0;
      8 	while (i < strlen(s) && s[i] == '*') i++;
      9 	return i;
     10 }
     11 TDescription str_desc = { .priority = count_stars };
     12 
     13 int main() {
     14 	PriorityQueue *q = NewPriorityQueue(str_desc);
     15 	char *s = "Boring task";
     16 	char *t = "* Eat dinner";
     17 	char *u = "**** Write blog post!";
     18 	Add(q, s);
     19 	Add(q, t);
     20 	Add(q, u);
     21 	char *first = Pop(q);
     22 	char *second = Pop(q);
     23 	printf("First thing to do is '%s', followed by '%s'\n", first, second);
     24 	return 0;
     25 }