TL;DR:mprintf("%s and %s", arg1, arg2)
似乎打印 "arg1arg2 and arg2"
而不是 “arg1 和 arg2”
使用 stdarg.h
va_*
宏
大家好。我正在使用 vsprintf 提供的功能,如下所示:
exits.c
#include "../../utils/printutil.c"
...
char dir[4][5];
char* format;
...
switch(bitcount) {
...
case 2: format = "%s and %s";
mprintf(format, dir[0], dir[1]);
break;
...
}
注意:dir
从 strcpy(dir[bitcount], names[dircount]);
获取其值,其中 names
只是一个字符指针数组,这样 dir[0] = "North"
, dir[1] = "East"
, dir[2] = South"
和 dir[3] = "West"
。
printutil.c
/* Utils file to provide common utilities not tied specifically to any aspect of the software */
#include "printutil.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char* mprintf(const char * format, ...) {
/* Prints a statement containing a format and then multiple arguments. */
char *buf;
va_list args;
va_start (args, format);
buf = malloc(sizeof(format) + sizeof(args));
vsprintf (buf, format, args);
va_end (args);
return buf;
}
注意:printutil.h
只包含函数原型(prototype)
这就是代码的结构。在 switch 语句的情况下,指定格式字符串,然后使用格式和参数 dir[0] 和 dir[1] 调用 mprintf()
(在 printutil.c 中定义) , 之前在 exits.c 中成功写入的两个变量。
使用 gdb,我能够辨别出传递给 mprintf()
的值符合预期:
Breakpoint 1, exitstr (exits=5 '\005') at exits.c:33
33 switch(bitcount) {
(gdb) s
38 case 2: format = "%s and %s";
(gdb) s
39 mprintf(format, dir[0], dir[1]);
(gdb) p format
$1 = 0x403115 "%s and %s"
(gdb) p dir[0]
$2 = "North"
(gdb) p dir[1]
$3 = "South"
当我进入 mprintf()
函数时,gdb 显示格式的内容与它们应该的完全一样,并且显示 va_list args
的内容:
17 vsprintf (buf, format, args);
(gdb) p format
$4 = 0x403115 "%s and %s"
(gdb) p args
$5 = {{gp_offset = 8, fp_offset = 48, overflow_arg_area = 0x7fffffffe710,
reg_save_area = 0x7fffffffe650}}
我根据在 cplusplus.com 引用中找到的示例构建了这段代码 vprintf和 vsprintf它们都表明我已经正确使用了 stdarg.h
中定义的函数和宏。
但是,在跨过 vsprintf()
行之后,打印 buf
的内容会产生问题的根源。也就是说,第二个参数看似连接到第一个参数,然后第二个参数被重新用于第二个格式说明符。
(gdb) print buf
$7 = 0x63ca50 "NorthSouth and South"
奇怪的是,这似乎只发生在“North”或“South”是第一个参数时。如果“East”或“West”是第一个参数,则参数会正确打印到 buf
。
提前感谢大家的时间和耐心。
最佳答案
buf = malloc(sizeof(format) + sizeof(args));
这是做什么用的? sizeof (format)
就是一个指针的大小,32 位系统为 4 个字节,64 位系统为 8 个字节。 sizeof (args)
只是sizeof (va_list)
的别名,它是一个实现定义的类型。但是,您将其用作字符串的预期大小。
您可能会溢出此缓冲区并遇到未定义的行为。
最好使用 snprintf
的变体,它采用指定的输出缓冲区大小。
编辑:此外,正如@Mahonri 所注意到的,您已将字符串 "North"
放入一个仅包含 5 个字符的空间的数组中,这会丢弃终止 NUL 字节.这导致 sprintf
超出字符串的预期结尾。我原以为它会打印 NorthEast
,但它仍然只是未定义的行为。
关于c - stdarg.h 功能似乎错误地连接参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23274800/