【问题标题】:Reading command line arguments into a new array in c?将命令行参数读入c中的新数组?
【发布时间】:2012-04-30 00:45:45
【问题描述】:

没有办法知道有多少参数;用户可以提供一个不确定长度的列表。

我的 C 语言很差。如何将参数从命令行数组中读取到新的字符串数组中?

老实说,我什至不知道如何制作一个单独的字符串数组。举个例子会很有帮助。

【问题讨论】:

  • 不能说我明白你的要求。通常main 的第一个参数是int,它包含命令行参数的数量。
  • 你的意思是从命令行获取参数吗?这些在argcargv 参数中传递给您的main() 函数。前者是一个包含参数计数的整数,后者是一个指向chars 的指针数组(即字符串)。
  • 你试过 google("c command line arguments") 吗?
  • 哦,我是个空想家。当然 argc 会告诉我他们给了多少。我累了;不要评判我。所以我不确定的是如何将一些数组参数的内容读入不同的数组。第一个 (argv[1]) 用于一件事,但其他所有内容都放在一起,我想将它分组到其他地方的新数组中。
  • 我无法想象为什么,但有人可能想将这些参数作为日志的一部分隐藏起来,以了解程序是如何被调用的。恕我直言,这是一个很好的问题,尽管有点独特。

标签: c arrays string char command-line-arguments


【解决方案1】:
int main ( int argc, char *argv[] ) {

}

这就是您声明主入口函数的方式。 argc 是用户输入的参数数量,包括程序名称,它是argv 中的第一个字符串(argv[0] = 程序名称)。

并且由于 argv 已经是一个字符串数组,我建议您使用它,因为这就是它的用途。 (只是不要忘记跳过索引 0)

【讨论】:

    【解决方案2】:

    是的。

    如果你看一下主函数的完整原型:

    int main(int argc, char **argv, char **env)
    

    argc:这是参数计数器,它包含用户给出的参数数量(假设命令是cd,输入cd home会得到argc = 2,因为命令名称总是参数 0)

    argv:这是参数值,它是一个大小为 argc 的数组,char* 指向参数本身。

    env:这是一个表(作为 argv),其中包含调用程序时的环境(例如,通过 shell,它使用 env 命令给出)。

    至于制作一个事物数组的示例两种方法都是可能的:

    一、定长数组:

    char tab[4]; // declares a variable "tab" which is an array of 4 chars
    tab[0] = 'a'; // Sets the first char of tab to be the letter 'a'
    

    二、变长数组:

    //You cannot do:
    //int x = 4;
    //char tab[x];
    //Because the compiler cannot create arrays with variable sizes this way
    //(If you want more info on this, look for heap and stack memory allocations
    //You have to do:
    int x = 4; //4 for example
    char *tab;
    tab = malloc(sizeof(*tab) * x); //or malloc(sizeof(char) * x); but I prefer *tab for
    //many reasons, mainly because if you ever change the declaration from "char *tab"
    //to "anything *tab", you won't have to peer through your code to change every malloc,
    //secondly because you always write something = malloc(sizeof(*something) ...); so you
    //have a good habit.
    

    使用数组:

    无论您选择声明它的方式(固定大小或可变大小),您始终以相同的方式使用数组:

    //Either you refer a specific piece:
    tab[x] = y; //with x a number (or a variable containing a value inside your array boundaries; and y a value that can fit inside the type of tab[x] (or a variable of that type)
    //Example:
    int x = 42;
    int tab[4]; // An array of 4 ints
    tab[0] = 21; //direct value
    tab[1] = x; //from a variable
    tab[2] = tab[0]; //read from the array
    tab[3] = tab[1] * tab[2]; //calculus...
    //OR you can use the fact that array decays to pointers (and if you use a variable-size array, it's already a pointer anyway)
    int y = 21;
    int *varTab;
    varTab = malloc(sizeof(*varTab) * 3); // An array of 3 ints
    *varTab = y; //*varTab is equivalent to varTab[0]
    varTab[1] = x; //Same as with int tab[4];
    *(varTab + 2) = 3; //Equivalent to varTab[2];
    //In fact the compiler interprets xxx[yyy] as *(xxx + yyy).
    

    给变量加星号称为取消引用。如果您不知道这是如何工作的,我强烈建议您看看。

    我希望对此进行了充分的解释。如果您仍有疑问,请发表评论,我将编辑此答案。

    【讨论】:

      【解决方案3】:
       int main(int argc, char *argv[]) 
      

      这就是你的程序中 main 的样子。 argc 是传递给程序的参数数量。 argv 是一个字符串列表,它们是传递给程序的参数,argv[0] 是程序名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-06
        • 2016-06-14
        • 2010-12-15
        • 1970-01-01
        • 2013-10-12
        • 2017-12-21
        • 2019-02-07
        • 2010-12-02
        相关资源
        最近更新 更多