I’m trying to set up an array of functions that takes in functions that need different variables (for example, my_putchar
needs a char
while my_put_nbr
needs an int
), so I just decided to define it using a void *
. While it does work it gives me warnings when I compile.
#include "stdarg.h"
#include "my.h"
int put_perc(void)
{
my_putchar('%');
}
int read_print(const char *s, int i, va_list args)
{
int k = 0;
char guide[] = "cdis%f";
int (*functions[])(void *) = {my_putchar, my_put_nbr,
my_put_nbr, my_putstr, put_perc, my_put_float};
for (k = 0; s[i + 1] != guide[k]; k++);
functions[k](va_arg(args, void *));
}
void mini_printf(const char *s, ...)
{
int k = 0;
va_list args;
char guide[] = "cdis%";
va_start(args, s);
for (int i = 0; i < my_strlen(s); i++) {
if (s[i] == '%') {
read_print(s, i, args);
i++;
} else {
my_putchar(s[i]);
}
}
va_end(args);
}
void main(void)
{
mini_printf("%s %s %s letter is %c followed by %d and %i.\nfloats %f \nlets also test this out, %s 100%%\n", "come", "on", "WORK", 'h', 58, 12, 55.756567575658, "this should work");
}
These are the warnings I get.
my_printf.c: In function ‘read_print’:
my_printf.c:20:35: warning: initialization of ‘int (*)(void *)’ from incompatible pointer type ‘void (*)(char)’ [-Wincompatible-pointer-types]
20 | int (*functions[])(void *) = {my_putchar, my_put_nbr,
| ^~~~~~~~~~
my_printf.c:20:35: note: (near initialization for ‘functions[0]’)
my_printf.c:20:47: warning: initialization of ‘int (*)(void *)’ from incompatible pointer type ‘int (*)(int)’ [-Wincompatible-pointer-types]
20 | int (*functions[])(void *) = {my_putchar, my_put_nbr,
| ^~~~~~~~~~
my_printf.c:20:47: note: (near initialization for ‘functions[1]’)
my_printf.c:21:9: warning: initialization of ‘int (*)(void *)’ from incompatible pointer type ‘int (*)(int)’ [-Wincompatible-pointer-types]
21 | my_put_nbr, my_putstr, put_perc, my_put_float};
| ^~~~~~~~~~
my_printf.c:21:9: note: (near initialization for ‘functions[2]’)
my_printf.c:21:21: warning: initialization of ‘int (*)(void *)’ from incompatible pointer type ‘int (*)(const char *)’ [-Wincompatible-pointer-types]
21 | my_put_nbr, my_putstr, put_perc, my_put_float};
| ^~~~~~~~~
my_printf.c:21:21: note: (near initialization for ‘functions[3]’)
my_printf.c:21:32: warning: initialization of ‘int (*)(void *)’ from incompatible pointer type ‘int (*)(void)’ [-Wincompatible-pointer-types]
21 | my_put_nbr, my_putstr, put_perc, my_put_float};
| ^~~~~~~~
my_printf.c:21:32: note: (near initialization for ‘functions[4]’)
my_printf.c:21:42: warning: initialization of ‘int (*)(void *)’ from incompatible pointer type ‘int (*)(double)’ [-Wincompatible-pointer-types]
21 | my_put_nbr, my_putstr, put_perc, my_put_float};
| ^~~~~~~~~~~~
my_printf.c:21:42: note: (near initialization for ‘functions[5]’)
I don’t really know how to fix this because I just thought void *
acted as a catchall for variable types. (this is only my 3rd week of coding so im still not that experienced)
You need to sign in to view this answers