Skip to content

Instantly share code, notes, and snippets.

@alex-robert-fr
Created November 4, 2022 16:28
Show Gist options
  • Save alex-robert-fr/8789a443e36fb4c0c9d0de609bf34cd3 to your computer and use it in GitHub Desktop.
Save alex-robert-fr/8789a443e36fb4c0c9d0de609bf34cd3 to your computer and use it in GitHub Desktop.
Singleton in C
// Header
#define MAX_FLAG 0xe
typedef struct s_printf t_printf;
struct s_printf
{
const char flag;
int (*cb)(const char *, int);
};
const t_printf *get_flag(const char flag);
// Source
const t_printf *get_flag(const char flag)
{
static const t_printf f[MAX_FLAG] = {
{ 's', ft_putstr_fd },
{ 'c', ft_putchar_fd },
};
int i;
i = 0;
while (i < MAX_FLAG)
{
if (f[i].flag == flag)
return (&f[i]);
i++;
}
return (NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment