Thread: Dumbed down version of printf recoding

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    1

    Dumbed down version of printf recoding

    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 !
    Last edited by Teehfox; 11-18-2016 at 04:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well making each of your my_... functions conform to this interface would be a start.
    > va_list (*ptrfunc)(va_list ap, char format);


    Your while loop needs to walk along str looking for %[scdi] characters, then use that char to search g_tab for the matching method.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making my own version of the scanf() version
    By Jamie_Edwards in forum C Programming
    Replies: 4
    Last Post: 04-30-2012, 09:46 AM
  2. recoding strings
    By nime in forum C Programming
    Replies: 17
    Last Post: 01-25-2011, 10:21 AM
  3. My own version of printf
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-19-2008, 08:38 AM
  4. Help with recoding Tar command
    By +Azazel+ in forum C Programming
    Replies: 14
    Last Post: 12-02-2007, 01:46 PM
  5. recoding getch()
    By JoshG in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2002, 02:36 AM

Tags for this Thread