- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的最小示例:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
void print_strings_and_lengths(int count, ...)
{
va_list ap;
/* Print strings */
va_start(ap, count);
for (int i = 0; i < count; i++) {
char *s = va_arg(ap, char *);
printf("%d - %s\n", i, s);
}
/* Print string lengths */
va_start(ap, count); /* Is it okay to call va_start() again without calling va_end()? */
for (int i = 0; i < count; i++) {
char *s = va_arg(ap, char *);
printf("%d - %zu\n", i, strlen(s));
}
va_end(ap);
}
int main()
{
print_strings_and_lengths(3, "apple", "ball", "cat");
return 0;
}
va_start()
在同一个变量参数列表上两次。
va_end()
在两次调用之间不调用函数。这段代码是定义明确的还是调用了未定义的行为?
最佳答案
C11 7.16.1/1:
[...] Each invocation of the
va_start
andva_copy
macros shall be matched by a corresponding invocation of theva_end
macro in the same function.
va_end
va_start
调用所以代码会导致未定义的行为,不需要诊断,因为上面的引用不是约束的一部分。
关于我们可以两次调用 va_start() 而不在其间调用 va_end() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608636/
我有一个基于 printf 样式格式的日志记录框架: void Logger::debug(const char *fmt, ...) { va_list args; va_start
我有以下代码: va_list va[2]; va_start(va[0], fmt); va_start(va[1], fmt); process(fmt, va); va_end(va[0]);
MSDN显示 vsnprintf_s 的示例代码片段: // crt_vsnprintf_s.cpp #include #include void FormatOutput(LPCSTR form
在处理可变参数时,是只是 va_start() 需要匹配的 va_end() 调用,还是 va_copy()也需要匹配的 va_end(),即 void foo(char *x, ...) { v
va_end - Macro to reset arg_ptr. 在访问变量参数列表后,arg_ptr 指针通常用va_end() 重置。我知道如果您想重新迭代列表,它是必需的,但如果您不打算这样做,
这是我的最小示例: #include #include #include void print_strings_and_lengths(int count, ...) { va_list
我试图在我的项目中使用 va_start 和 va_end 函数,但 eclipse 不想将其解析为函数。 gcc 编译整个项目没有错误... [我的文件.cpp] #include #includ
我有一个封装 CString::FormatV 的函数函数,并且我需要能够检测是否向该函数传递了一个空参数列表。执行此操作的最佳方法是什么? 我现在的代码是这样的 CString ADString::
(一)写一个简单的可变参数的C函数 下面我们来探讨如何写一个简单的可变参数的C函数.写可变参数的C函数要在程序中用到以下这些宏: 复制代码 代码如下: void va_start(
我需要使用 %%在格式字符串中以便写入 %像这样的字符: printf("%%"); 我正在尝试在我的可变参数列表函数中执行此操作。格式说明符工作正常,例如 %d , %s等等 但是当我尝试写 %字符
如果我的理解是正确的,va_end 被调用以进行清理并且在相同的上下文中,va_end 的手册页说明了这一点 va_end() Each invocation of va_start() must b
在此问答中,您应始终调用 va_end(): What exactly is va_end for? Is it always necessary to call it? 但是如果在到达 va_end
由于在C语言中没有函数重载,解决不定数目函数参数问题变得比较麻烦,即使采用C++,如果参数个数不能确定,也很难采用函数重载。对这种情况,提出了指针参数来解决问题。 如printf()函数,其原型为
这是我的代码。它们是Objective-C语言: - (void)objcMethod:(NSString *)format, ... { va_list args; va_start(args, f
我使用下一个函数: void CharTest(const char * format,...) { char buffer[1024]; va_list args; va_star
谁能告诉我如何使用 va_start、va_end、 实现 printf 或 scanf va_arg 和系统调用读/写应该是什么样子? 我有这样的东西: #include #include ch
我的代码包含如下片段: std::va_list ap; va_start(ap, msgfmt); snprintf_buf buf; const tchar * m
我是一名优秀的程序员,十分优秀!