From e6d8091dafc3da444e1583a872f3dd5fd2cf84b1 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 20 Aug 2023 20:16:46 +0300 Subject: [PATCH] I can't even begin to address what was wrong here 1. space missing in #include 2. moved declarations above all so betty approves 3. No more mid code variables 4. malloced the buffer initially so its actually on the heap before freeing 5. added a missing * for the error that was upsetting in the bottom 6. Returning the value of strlen instead of identifiers --- printf.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/printf.c b/printf.c index a40068e..be5c0e3 100644 --- a/printf.c +++ b/printf.c @@ -1,31 +1,30 @@ #include "main.h" -#include +#include +#include int _printf(const char *format, ...) { /* For any Iterative Operation */ - int i = 0; + int i, pass_str_len; + unsigned int identifiers, BUFF_SIZE; + char *buffer; + va_list args; - /* how many %* that aren't %% */ - unsigned int identifiers; - - /* Number of Identifiers */ - identifiers = _contains(format, '%'); - - /* Primary Buffer Size */ - int BUFF_SIZE = _strlen(format) - (identifiers * 2); - - /* Instantiating Buffer */ - char* buffer[BUFF_SIZE]; + /* Instantiating Buffer Size */ + BUFF_SIZE = _strlen(format) - (identifiers * 2); /* Shoving args in list */ - va_list args; va_start(args, format); + /* Number of Identifiers that aren't "%%" */ + identifiers = _contains(format, '%'); + /* If string is nonexist, die */ if (!format) return (0); + buffer = malloc(BUFF_SIZE); + /* While string is alive or smth*/ while(format) { @@ -43,7 +42,7 @@ int _printf(const char *format, ...) */ /* Trying to get length of passed string */ - int pass_str_len = _strlen(va_arg(args, char*)); + pass_str_len = _strlen(va_arg(args, char*)); /* Incrememnting buffersize to fit new string*/ BUFF_SIZE += pass_str_len; @@ -80,11 +79,11 @@ int _printf(const char *format, ...) } else { - /* ERROR SCREAMING AT ME PLS HELP */ - *(buffer + i) = (char *)(format + i); + /* Dereferencing with '*' */ + *(buffer + i) = *(char *)(format + i); i++; } } - return (identifiers); + return (_strlen(buffer)); }