简体   繁体   English

[C]我可以使用比参数更大的类型调用va_arg,然后强制转换值吗?

[英][C]Can I call va_arg with a larger type than the argument, then cast the value?

I wonder if this is valid code: 我想知道这是否是有效的代码:

int a(va_list args, int is_int, ...)
{
    long long n = va_arg(args, long long);
    return (is_int ? ((int)n) : n);
}
void b(va_list args)
{
    int x = -100;
    a(args, 1, x);
}

I've written some unnecessary complicated code, I've made mistakes, and I feel like this is the simplest way to fix what I did without rewriting the whole thing, but I don't know if this can cause errors(or segfaults, or something else). 我写了一些不必要的复杂代码,犯了错误,我觉得这是修正我所做的事情的最简单方法,而无需重写整个过程,但是我不知道这是否会导致错误(或段错误,或者是其他东西)。

No, and the issue is not just limited to size. 不,问题不仅限于大小。 If the types mismatch, the behavior of your whole program is undefined. 如果类型不匹配,则整个程序的行为是不确定的。 There are some very limited exceptions like signedness mismatch when the actual value passed fits in either signedness. 当传递的实际值适合任一正负号时,有一些非常有限的例外,例如正负号不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM