c - main 中的可变参数列表

标签 c c99 variadic-functions

我想像这样使用我的程序:

./program -I /usr/include/ /usr/bin/ /usr/local/include/ ...

开关可以像在 var args 列表中一样持续不断。我怎么能在 C99 中做到这一点?最好得到像 char **args_listchar *args_list[] 这样的东西,它包含所有像 /usr/include >/usr/bin/

最佳答案

运行以下代码的输出:

int main(int argc, char* argv[])
{
    for (int i = 1; i < argc; ++i)
    {
        printf("%s\n", argv[i]);
    }
}

program -I/usr/include//usr/bin//usr/local/include/ 执行

输出:

-I
/usr/include/
/usr/bin/
/usr/local/include/

请注意,在代码示例中,初始索引为 1。这是因为 argv 变量中的第一个指针是程序的名称。在这种情况下,它将是 program

关于c - main 中的可变参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3783331/

相关文章:

c - 当循环迭代不独立时使用 OpenMP 并行化循环

对 strcpy 的这个实现感到困惑,为什么需要三个临时变量?

c - strcmp 无法正确比较字符串

arguments - Pascal - 如何将可变数量的参数传递给子程序? (可变函数)

循环创建、连接信号到按钮

c - `uint_fast32_t` 是否保证至少与 `int` 一样宽?

c - mbtowc : howto determine number of characters to skip if null character is read

c - 这两个定义有什么区别

c++ - 可变参数模板,获取函数参数值

c++ - 为什么gcc从这里的回调函数的最后一个参数推断类型?