C-将可变数量的命令行参数传递给具有可变数量参数的方法 [英] C - Pass variable number of command line arguments into method with variable number of parameters

查看:85
本文介绍了C-将可变数量的命令行参数传递给具有可变数量参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C程序,该程序将使用可变数量的命令行参数。然后,我需要将这些参数传递给一个函数,该函数将可变数量的文件名作为单个参数(使用va_arg在函数内部获取参数),原型为:

I am writing a C program that will take a variable number of command line arguments. I need to then take these arguments and pass them into a function that takes a variable number of filenames as individual parameters (using va_arg to get the arguments inside the function), prototyped as:

void FindFile(char *filename1, ...);

我的FindFile实现很好。我的问题是,如何在主方法的'char * argv []'中获取可变数量的参数,并在调用FindFile时将它们用作参数?

My implementation of FindFile is fine. My question is, how do I take the variable number of arguments in the main method's 'char *argv[]' and use them as parameters when calling FindFile?

这是类的分配,因此无法更改FindFile原型。我一直在寻找使这项工作可行的方法,只找到一个答案,这说不可能。确实是这样吗?这是我教授给出的确切说明,所以我以某种方式认为这是可能的,但是在课堂上没有讨论确切的方法。

This is an assignment for a class, so the FindFile prototype can not be changed. I have searched for ways to make this work, only finding one answer, which said that it is impossible to do. Is this actually the case? It is the exact specification given by my professor so I assumed it would be possible somehow, but the exact method was not discussed in class.

推荐答案

我已经在我的邮编解释器中做了类似的事情

这是从我的操作员处理函数中获得的。它使用switch语句通过函数指针进行 variadic调用

This is from my operator-handler function. It uses a switch statement to make the variadic call through the function pointer.

call: /* pass args bottom-up */
    tos = siq; /* pop the stack to the 'stack in question' */
    /* room for output? */
    if (tos-os + op.sig[i].out > OSSIZE) error(st,stackoverflow);
    switch (op.sig[i].n) {
        case 0: op.sig[i].fp(st); break;
        case 1: op.sig[i].fp(st, siq[0]); break;
        case 2: op.sig[i].fp(st, siq[0], siq[1]); break;
        case 3: op.sig[i].fp(st, siq[0], siq[1], siq[2]); break;
        case 4: op.sig[i].fp(st, siq[0], siq[1], siq[2], siq[3]); break;
        case 5: op.sig[i].fp(st, siq[0], siq[1], siq[2], siq[3], siq[4]); break;
        case 6: op.sig[i].fp(st, siq[0], siq[1], siq[2], siq[3], siq[4], siq[5]); break;
        default: error(st,unregistered);
    }

我从 Goswell解释器(又名Rutherford解释器,又名RALpage)

这篇关于C-将可变数量的命令行参数传递给具有可变数量参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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