如何传递主的*的argv []一个函数? [英] How can I pass main's *argv[] to a function?

查看:174
本文介绍了如何传递主的*的argv []一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以接受命令行参数,我想访问的参数,用户输入,从功能。我怎样才能通过 *的argv [] ,从 INT主(INT ARGC,CHAR *的argv [])来该功能?我有点新指针的概念和 *的argv [] 看起来有点太复杂,我的工作这一点上我自己的。

I have a program that can accept command-line arguments and I want to access the arguments, entered by the user, from a function. How can I pass the *argv[], from int main( int argc, char *argv[]) to that function ? I'm kind of new to the concept of pointers and *argv[] looks a bit too complex for me to work this out on my own.

我们的想法是离开我的通过移动所有的工作,我想要做的参数,到库文件尽可能干净。我已经知道我有什么用这些参数来办时,我设法让他们抓住外。我只是不知道如何让他们在那里。

The idea is to leave my main as clean as possible by moving all the work, that I want to do with the arguments, to a library file. I already know what I have to do with those arguments when I manage to get hold of them outside the main. I just don't know how to get them there.

我使用的GCC。
先谢谢了。

I am using GCC. Thanks in advance.

推荐答案

只是写一个函数,如

void parse_cmdline(int argc, char *argv[])
{
    // whatever you want
}

和调用,在 parse_cmdline(ARGC,ARGV)。没有神奇的参与。

and call that in main as parse_cmdline(argc, argv). No magic involved.

其实,你并不真的需要通过 ARGC ,因为的argv 的最后一个成员是有保障是一个空指针。但是,既然你有 ARGC ,你还不如把它传递。

In fact, you don't really need to pass argc, since the final member of argv is guaranteed to be a null pointer. But since you have argc, you might as well pass it.

如果该功能不需要知道程序名,你也可以决定称其为

If the function need not know about the program name, you can also decide to call it as

parse_cmdline(argc - 1, argv + 1);

这篇关于如何传递主的*的argv []一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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