M
m.labanowicz
Hello,
I'm trying to compile following example:
-----{test:BEG}----------------------------------------
01: #include <stdio.h>
02: #include <stdarg.h>
03: #include <stdlib.h>
04: int va_get_int(va_list * ap) {
05: return va_arg(*ap, int);
06: }
07: char * va_get_chrptr(va_list * ap) {
08: return va_arg(*ap, char *);
09: }
10: void fooA(va_list ap) {
11: char const * txt = NULL;
12: printf("A:int = %d\n", va_arg(ap, int));
13: txt = va_arg(ap, char *);
14: printf("Atr = %p\n", (void *)txt);
15: printf("Atr = \"%s\"\n", txt);
16: }
17: void fooB(va_list ap) {
18: char const * txt = NULL;
19: printf("B:int = %d\n", va_get_int(&ap));
20: txt = va_get_chrptr(&ap);
21: printf("Btr = %p\n", (void *)txt);
22: printf("Btr = \"%s\"\n", txt);
23: }
24: void bar(char const * arg0, ...) {
25: va_list ap;
26: printf("arg0 = \"%s\"\n", arg0);
27: va_start(ap, arg0);
28: fooA(ap);
29: va_end(ap);
30: va_start(ap, arg0);
31: fooB(ap);
32: va_end(ap);
33: va_start(ap, arg0);
34: fooA(ap);
35: va_end(ap);
36: }
37: int main(void) {
38: bar("koko", 23, "txt");
39: return EXIT_SUCCESS;
40: }
-----{test:EOF}----------------------------------------
[32 bit machine]
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
$ gcc -W -Wall -ansi -pedantic test.c
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x8048669
Atr = "txt"
B:int = 23
Btr = 0x8048669
Btr = "txt"
A:int = 23
Atr = 0x8048669
Atr = "txt"
[64 bit machine]
gcc (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2
$ gcc -W -Wall -ansi -pedantic test.c
test.c: In function 'fooB':
test.c:18:3: warning: passing argument 1 of 'va_get_int' from incompatible pointer type [enabled by default]
test.c:4:5: note: expected 'struct __va_list_tag (*)[1]' but argument is of type 'struct __va_list_tag **'
test.c:19:3: warning: passing argument 1 of 'va_get_chrptr' from incompatible pointer type [enabled by default]
test.c:7:8: note: expected 'struct __va_list_tag (*)[1]' but argument is of type 'struct __va_list_tag **'
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x40096d
Atr = "txt"
B:int = -443987883
Btr = 0x455441ed31455541
Segmentation fault (core dumped)
It seems that pointer to 'va_list' can not be used,
ok, I have changed code to:
-----{diff:BEG}----------------------------------------
4,5c4,5
< int va_get_int(va_list * ap) {
< return va_arg(*ap, int);
---
< char * va_get_chrptr(va_list * ap) {
< return va_arg(*ap, char *);
---
< printf("B:int = %d\n", va_get_int(&ap));
< txt = va_get_chrptr(&ap);
---
After that both compilers are silent,
no errors, no warnings, but:
[64 bit machine]
gcc (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2
$ gcc -W -Wall -ansi -pedantic test2.c
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x40096d
Atr = "txt"
B:int = 23
Btr = 0x40096d
Btr = "txt"
A:int = 23
Atr = 0x40096d
Atr = "txt"
[32 bit machine]
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
$ gcc -W -Wall -ansi -pedantic test2.c
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x8048669
Atr = "txt"
B:int = 23
Btr = 0x17
Segmentation fault
Best Regards
I'm trying to compile following example:
-----{test:BEG}----------------------------------------
01: #include <stdio.h>
02: #include <stdarg.h>
03: #include <stdlib.h>
04: int va_get_int(va_list * ap) {
05: return va_arg(*ap, int);
06: }
07: char * va_get_chrptr(va_list * ap) {
08: return va_arg(*ap, char *);
09: }
10: void fooA(va_list ap) {
11: char const * txt = NULL;
12: printf("A:int = %d\n", va_arg(ap, int));
13: txt = va_arg(ap, char *);
14: printf("Atr = %p\n", (void *)txt);
15: printf("Atr = \"%s\"\n", txt);
16: }
17: void fooB(va_list ap) {
18: char const * txt = NULL;
19: printf("B:int = %d\n", va_get_int(&ap));
20: txt = va_get_chrptr(&ap);
21: printf("Btr = %p\n", (void *)txt);
22: printf("Btr = \"%s\"\n", txt);
23: }
24: void bar(char const * arg0, ...) {
25: va_list ap;
26: printf("arg0 = \"%s\"\n", arg0);
27: va_start(ap, arg0);
28: fooA(ap);
29: va_end(ap);
30: va_start(ap, arg0);
31: fooB(ap);
32: va_end(ap);
33: va_start(ap, arg0);
34: fooA(ap);
35: va_end(ap);
36: }
37: int main(void) {
38: bar("koko", 23, "txt");
39: return EXIT_SUCCESS;
40: }
-----{test:EOF}----------------------------------------
[32 bit machine]
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
$ gcc -W -Wall -ansi -pedantic test.c
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x8048669
Atr = "txt"
B:int = 23
Btr = 0x8048669
Btr = "txt"
A:int = 23
Atr = 0x8048669
Atr = "txt"
[64 bit machine]
gcc (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2
$ gcc -W -Wall -ansi -pedantic test.c
test.c: In function 'fooB':
test.c:18:3: warning: passing argument 1 of 'va_get_int' from incompatible pointer type [enabled by default]
test.c:4:5: note: expected 'struct __va_list_tag (*)[1]' but argument is of type 'struct __va_list_tag **'
test.c:19:3: warning: passing argument 1 of 'va_get_chrptr' from incompatible pointer type [enabled by default]
test.c:7:8: note: expected 'struct __va_list_tag (*)[1]' but argument is of type 'struct __va_list_tag **'
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x40096d
Atr = "txt"
B:int = -443987883
Btr = 0x455441ed31455541
Segmentation fault (core dumped)
It seems that pointer to 'va_list' can not be used,
ok, I have changed code to:
-----{diff:BEG}----------------------------------------
4,5c4,5
< int va_get_int(va_list * ap) {
< return va_arg(*ap, int);
---
7,8c7,8int va_get_int(va_list ap) {
return va_arg(ap, int);
< char * va_get_chrptr(va_list * ap) {
< return va_arg(*ap, char *);
---
19,20c19,20char * va_get_chrptr(va_list ap) {
return va_arg(ap, char *);
< printf("B:int = %d\n", va_get_int(&ap));
< txt = va_get_chrptr(&ap);
---
-----{diff:EOF}----------------------------------------printf("B:int = %d\n", va_get_int(ap));
txt = va_get_chrptr(ap);
After that both compilers are silent,
no errors, no warnings, but:
[64 bit machine]
gcc (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2
$ gcc -W -Wall -ansi -pedantic test2.c
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x40096d
Atr = "txt"
B:int = 23
Btr = 0x40096d
Btr = "txt"
A:int = 23
Atr = 0x40096d
Atr = "txt"
[32 bit machine]
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
$ gcc -W -Wall -ansi -pedantic test2.c
$ ./a.out
arg0 = "koko"
A:int = 23
Atr = 0x8048669
Atr = "txt"
B:int = 23
Btr = 0x17
Segmentation fault
Best Regards