Hello, I wanted to use a little exercise to train myself into using pointers on functions in C, for that, I started a dumbed down version of printf, only handling a few flags, nothing more, but i've come to a point where i'm stuck, and can't figure out what to do next, I'm not the sharpest tool in the shed and feel like it's dumb, but could you guys help me ? Here's the code, my problem is with what i'm supposed to put in the while, but I might have made mistakes on some other places, correct me if i'm wrong, and please oh please explain it to me :
Code:
#include "printf.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void my_printf(char *str, ...)
{
int i;
va_list ap;
t_func g_tab[] =
{
{&my_putstr, 's'},
{&my_putchar, 'c'},
{&my_putnbr, 'd'},
{&my_putnbr, 'i'},
{NULL, -1)
};
va_start(ap, str);
while (g_tab[i].key != -1)
{
}
va_end(ap, str);
}
Here is my .h mentionned in the includes :
Code:
#ifndef PRINTF_H
#define PRINTF_H
typedef struct s_func
{
va_list (*ptrfunc)(va_list ap, char format);
char key;
} t_func;
#endif
And here are my basic functions used to process what printf would receive :
Code:
void my_putchar(char c)
{
write(1, &c, 1);
}
void my_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
my_putchar(str[i]);
i++;
}
}
int my_putnbr(int nb)
{
int i;
if (nb < 0)
{
my_putchar('-');
nb = nb * -1;
}
if (nb >= 0)
{
if (nb >= 10)
{
i = (nb % 10);
nb = (nb - i) / 10;
my_put_nbr(nb);
my_putchar(48 + i);
}
else
my_putchar(48 + nb % 10);
}
}
Thanks a lot already !