Bonjour,
J'essaye actuellement de recréer la fonction printf, mais je rencontre quelques problèmes quant à l'utilisation de va_arg, à la compilation j'obtiens les erreurs suivantes:
Je compile en utilisant gcc -Wall -Wextra -Werror -pedantic -std=gnu89 -Wno-format *.c et pour le coup, j'arrive à comprendre qu'il n'aime pas mais je vois pas comment régler le problème... Voici les différents fichiers
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 In file included from main.h:4, from _printf.c:1: _printf.c: In function _printf: _printf.c:26:46: error: passing argument 1 of f makes pointer from integer without a cast [-Werror=int-conversion] 26 | nb_char += f(va_arg(ptr, int)); | ^ | | | int _printf.c:26:46: note: expected __va_list_tag * but argument is of type int _printf.c:29:46: error: passing argument 1 of f from incompatible pointer type [-Werror=incompatible-pointer-types] 29 | nb_char += f(va_arg(ptr, char *)); | ^ | | | char * _printf.c:29:46: note: expected __va_list_tag * but argument is of type char * _printf.c:32:46: error: passing argument 1 of f makes pointer from integer without a cast [-Werror=int-conversion] 32 | nb_char += f(va_arg(ptr, int)); | ^ | | | int _printf.c:32:46: note: expected __va_list_tag * but argument is of type int cc1: all warnings being treated as errors
main.h
main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #ifndef MAIN_H #define MAIN_H #include <stddef.h> #include <stdarg.h> #include <unistd.h> /** * struct specifier - Struct specifier * @specifier: The specifier * @f: The function associated */ typedef struct specifier { char *spe; int (*f)(va_list); } spec; int _printf(const char *format, ...); int print_char(va_list); int print_string(va_list); int print_integer(va_list); int (*get_functions(char s))(va_list); #endif /* MAIN_H */
print.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 #include <stdio.h> #include "main.h" int main(void) { int len; len = _printf("Let's try to printf a simple sentence.\n"); _printf("Length:[%d]\n", len); return (0); }
_printf.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 #include "main.h" int print_char(va_list arg) { char c = va_arg(arg, int); return (write(1, &c, 1)); } int print_string(va_list arg) { int i, nb_char = 0; char *str = va_arg(arg, char *); for (i = 0; str[i]; i++) { write(1, &str[i], 1); nb_char++; } return (nb_char); } int nb_columns(int n) { int len = 1; while (n / 10) { n /= 10; len *= 10; } return (len); } int print_integer(va_list arg) { int nb_char = 0, column, base, c; int n = va_arg(arg, int); if (n < 0) { if (n == -2147483648) { base = n; n = -(n + 1); } else n = -n; write(1, "-", 1); nb_char++; } column = nb_columns(n); while (column) { if (column == 1 && base == -2147483648) write(1, "8", 1); else { c = n / column + '0'; write(1, &c, 1); } nb_char++; n %= column; column /= 10; } return (nb_char); }
get_functions.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #include "main.h" int _printf(const char *format, ...) { int nb_char = -1; if (format) { int (*f)(va_list); va_list ptr; va_start(ptr, format); nb_char = 0; for (; *format; format++) { while (*format != '%') { write(1, &format, 1); nb_char++; } format++; f = get_functions(*format); switch (*format) { case 'c': nb_char += f(va_arg(ptr, int)); break; case 's': nb_char += f(va_arg(ptr, char *)); break; case 'd': nb_char += f(va_arg(ptr, int)); break; } } va_end(ptr); } return (nb_char); }
Merci d'avance pour votre aide !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 #include "main.h" /** * get_functions - get the specifier * @s: specifier * Return: pointer to the function to use */ int (*get_functions(char s))(va_list) { spec c[] = { {"c", print_char}, {"s", print_string}, {"%", NULL}, {"d", print_integer}, {"i", NULL}, {"b", NULL}, {"u", NULL}, {"o", NULL}, {"x", NULL}, {"X", NULL}, {"S", NULL}, {"p", NULL}, {"+", NULL}, {" ", NULL}, {"#", NULL}, {"l", NULL}, {"h", NULL}, {"0", NULL}, {"-", NULL}, {"r", NULL}, {"R", NULL}, {NULL, NULL} }; int i = 0; while (i < 22) { if (s == *c[i].spe) return (c[i].f); i++; } return (NULL); }
Partager