I can't even begin to address what was wrong here

1. space missing in #include <stdarg.h>
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
This commit is contained in:
LinlyBoi
2023-08-20 20:16:46 +03:00
parent cc9901e281
commit e6d8091daf

View File

@@ -1,31 +1,30 @@
#include "main.h" #include "main.h"
#include<stdarg.h> #include <stdarg.h>
#include <stdlib.h>
int _printf(const char *format, ...) int _printf(const char *format, ...)
{ {
/* For any Iterative Operation */ /* 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 %% */ /* Instantiating Buffer Size */
unsigned int identifiers; BUFF_SIZE = _strlen(format) - (identifiers * 2);
/* Number of Identifiers */
identifiers = _contains(format, '%');
/* Primary Buffer Size */
int BUFF_SIZE = _strlen(format) - (identifiers * 2);
/* Instantiating Buffer */
char* buffer[BUFF_SIZE];
/* Shoving args in list */ /* Shoving args in list */
va_list args;
va_start(args, format); va_start(args, format);
/* Number of Identifiers that aren't "%%" */
identifiers = _contains(format, '%');
/* If string is nonexist, die */ /* If string is nonexist, die */
if (!format) if (!format)
return (0); return (0);
buffer = malloc(BUFF_SIZE);
/* While string is alive or smth*/ /* While string is alive or smth*/
while(format) while(format)
{ {
@@ -43,7 +42,7 @@ int _printf(const char *format, ...)
*/ */
/* Trying to get length of passed string */ /* 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*/ /* Incrememnting buffersize to fit new string*/
BUFF_SIZE += pass_str_len; BUFF_SIZE += pass_str_len;
@@ -80,11 +79,11 @@ int _printf(const char *format, ...)
} }
else else
{ {
/* ERROR SCREAMING AT ME PLS HELP */ /* Dereferencing with '*' */
*(buffer + i) = (char *)(format + i); *(buffer + i) = *(char *)(format + i);
i++; i++;
} }
} }
return (identifiers); return (_strlen(buffer));
} }