简体   繁体   English

如果我使用结构来承载argc和argv,如何将地址argv分配给结构内的变量?

[英]If I use a struct to carry argc and argv, how do I assign the address argv to a variable inside the structure?

I have defined some structure above main as: 我在main上方定义了一些结构:

struct arguments
{
   int c;
   char *v[];
};

now I want to do this in main: 现在我主要要这样做:

int main(int argc, char *argv[])
{
    arguments arg;

    arg.c = argc;
    arg.v = argv; /* error: incompatible types in assignment of 'char** to char* [0]' */
}

So, I do not fully understand what I am doing, but instinctively I remade the structure such that the line char *v[]; 因此,我不完全了解我在做什么,但是本能地我重新构造了结构,使得char *v[]; is instead char **v[]; 而是char **v[]; however, this means when I pass by reference my structure arg , if I want to dereference and get the value of arg.v[0] , which will be the program name, I now have to do *arg.v[0] and this no longer works for *arg.v[1] . 但是,这意味着当我通过引用传递我的结构arg ,如果我想取消引用并获取arg.v[0]的值(它将是程序名称),则现在必须执行*arg.v[0]和这不再适用于*arg.v[1]

I have a function such as: 我有一个功能,如:

void argument_reader(arguments &arg)
{
   cout << "Number of arguments: " << arg.c << endl << endl;
   cout << "Array\t\tValue\n";
   cout_bar(40);
   for (int i = 0; i < arg.c; i++)
   {
      cout << "argv[" << i << "]\t\t" << *arg.v[i] << endl;
   }
   return;
}

but when I call *arg.v[i] the program ends without printing the value of the second argument or any others for that matter. 但是,当我调用*arg.v[i] ,程序结束,而没有打印第二个参数或其他任何参数的值。

So my question is, is there some way I can use the struct to pass between functions such that I can call arg.v[some_index < arg.c] inside another function by somehow making the line arg.v = argv in main work as I had intended (to store the address of argv in arg.v ) 所以我的问题是,有什么方法可以使用结构在函数之间传递,以便可以通过以某种方式使arg.v = argvarg.v = argv主要工作,从而在另一个函数内调用arg.v[some_index < arg.c]我原本打算(将argv的地址存储在arg.v

All the structure does is make it so that instead of having to pass argc and argv to a function which has a decleration like: void func(int &argc, char *argv[]); 所有结构所做的就是使它变得不必将argcargv传递给一个具有declecle的函数,例如: void func(int &argc, char *argv[]); I can instead pass my new structure type with variable name arg to a function declared like void func(arguments &arg) and that is basically all I wanted the structure for. 相反,我可以将带有变量名arg新结构类型传递给一个声明为void func(arguments &arg) ,这基本上就是我想要的结构。 So if I cannot do this, then I can go back to the first method. 因此,如果无法执行此操作,则可以返回第一种方法。

You need to write: 您需要写:

struct arguments
{
    int    c;
    char **v;
};

Now your main() will work. 现在,您的main()将起作用。 (And this sort of confusion is why I always use int main(int argc, char **argv) , but that's a personal quirk.) (这就是为什么我总是使用int main(int argc, char **argv) ,但这是个人的怪癖。)

If you were working in C99 or later, what you created with your structure is what is called a 'flexible array member' (FAM), an addition to C99 over C89 (and therefore over C++98). 如果您使用的是C99或更高版本,则使用结构创建的就是所谓的“灵活数组成员” (FAM),它是C89上C89(因此是C ++ 98上)的附加功能。 I didn't notice that you're working in C++! 我没有注意到您正在使用C ++! There are special rules about FAM in C, and you can't do the assignment as you did with a flexible array member. 关于C中FAM的特殊规则,您不能像使用灵活数组成员那样进行分配。

However, the fix will work in C++ just as much as in C; 但是,此修复程序在C ++中的工作原理与在C语言中一样。 it is just not necessarily correct that you're using an FAM. 使用FAM不一定完全正确。 Are you using a C++ compiler that supports FAM as an extension? 您是否正在使用支持FAM作为扩展的C ++编译器?

In your output line, you have: 在输出行中,您具有:

cout << "argv[" << i << "]\t\t" << *arg.v[i] << endl;

That would print the first character of the argument; 那会打印出参数的第一个字符; drop the * and you'd get the first argument as a string: 删除* ,您将获得第一个参数作为字符串:

cout << "argv[" << i << "]\t\t" << arg.v[i] << endl;

Change your struct to this: 将您的结构更改为此:

struct arguments
{
   int c;
   char **v;
};

C-style array and pointer are very similar, and you can use above struct just like your version or the original argv parameter. C样式的数组和指针非常相似,您可以像使用版本或原始argv参数一样使用上述结构。

Indeed, often main function is declared like that instead of using []: 确实,通常这样声明main函数,而不是使用[]:

int main(int argc, char **argv)

Note that with this answer code v points to the original argv array of pointers to char, pointers are not copied. 请注意,使用此答案代码v指向指向char的指针的原始argv数组时,不会复制指针。 If you actually want a copy, you need to dynamically allocate array of char pointers and copy. 如果您实际上想要一个副本,则需要动态分配char指针数组并进行复制。

two things: 两件事情:

struct arguments
{
   int c;
   char *v[]; /*unknown length, hence structure is incomplete*/
};

You need a double pointer, which holds the address of the argv 您需要一个双指针,其中包含argv的地址

    struct arguments {
       int c;
       char **v;
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM