Skip to content

Instantly share code, notes, and snippets.

@JeGa
Created November 9, 2014 11:54
Show Gist options
  • Save JeGa/fc2124e0c0586a957130 to your computer and use it in GitHub Desktop.
Save JeGa/fc2124e0c0586a957130 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <windows.h> // Windows sleep
#define TABLE_SIZE 3
typedef enum {TYPE_INT, TYPE_LONG, TYPE_CHAR} dataType;
typedef struct {
void *data;
dataType type;
} dataTable;
// Global for simplicity
static dataTable table[TABLE_SIZE];
static int var1 = 10;
static long var2 = 20;
static char var3 = 30;
// ===================================================================
void checkType(void *data, dataType type)
{
if (type == TYPE_INT)
printf("integer: %d\n", *((int *) data));
else if (type == TYPE_LONG)
printf("long: %ld\n", *((long *) data));
else if (type == TYPE_CHAR)
printf("char: %d\n", *((char *) data));
}
// "Main task"
void mainTask()
{
var1 += 1;
var2 += 1;
var3 += 1;
}
// "Task"
void task()
{
int i;
printf("== Task ==\n");
for (i = 0; i < TABLE_SIZE; ++i)
checkType(table[i].data, table[i].type);
}
// Task with moving pointer
void ptrTask()
{
static dataTable *current = table;
printf("== Task with moving pointer ==\n");
checkType(current->data, current->type);
if (current == &table[TABLE_SIZE - 1])
current = table;
else
++current;
}
int main(int argc, char **argv)
{
// Init
table[0].data = &var1;
table[0].type = TYPE_INT;
table[1].data = &var2;
table[1].type = TYPE_LONG;
table[2].data = &var3;
table[2].type = TYPE_CHAR;
// Task
while(1) {
task();
ptrTask();
Sleep(500); // Windows sleep
mainTask();
Sleep(500); // Windows sleep
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment