Skip to content

Instantly share code, notes, and snippets.

@sphynx
Last active December 18, 2018 23:21
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 sphynx/becea49e70948f2bcaea87926708de46 to your computer and use it in GitHub Desktop.
Save sphynx/becea49e70948f2bcaea87926708de46 to your computer and use it in GitHub Desktop.
Example of functions inside global structures, emulating "Serial" from Arduino language.
#include <stdio.h>
#include <stdlib.h>
typedef void (*func) (const char *);
struct module {
func print;
func println;
};
static void println(const char *str)
{
puts(str);
}
static void print(const char *str)
{
printf("%s", str);
}
static struct module Serial = { print, println };
int main(void)
{
Serial.print("hello, ");
Serial.print("world!");
Serial.println("");
Serial.println("exiting...");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment