可变参数 C 函数中的空值 [英] Nulls in variadic C functions

查看:32
本文介绍了可变参数 C 函数中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义了一个可变参数函数:

If I defined a variadic function:

#include <stdio.h>
#include <stdarg.h>

int f(char*s,...)
{ 
    va_list ap;
    int i=0;
    va_start(ap, s);
    while(s)
    {
       printf("%s ", s);
       i++;
       s=va_arg(ap,char*);
    }
    va_end(ap);
    return i;
}

int main()
{ 
    return f("a","b",0);
}

gcc (linux x64) 编译这个,exe 运行并打印a b".

gcc (linux x64) compiles this and the exe runs and prints "a b ".

是否需要演员阵容:

return f("a","b",(char*)0)

在通用系统上?

推荐答案

您的代码应该可以在任何支持 SYSV x64 ABI 标准.它可能适用于使用具有类似要求的其他 ABI 标准的系统,并且可能在使用其他 ABI1 的任何系统上失败.根据 C 标准,这都是未定义的.

Your code should work fine on any system that supports the SYSV x64 ABI standard. It may work on systems that use some other ABI standard that has similar requirements, and may fail on any system that uses some other ABI1. This is all undefined according to the C standard.

1特别是,微软不遵循标准的 x64 ABI,他们有自己的标准.

这篇关于可变参数 C 函数中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆