Manually construct parameters that are equivalent to ' char *argv[] '

Source: Internet
Author: User

Scene

The two parameters in the main function are (int argc , char *argv[]) constructed by the system. Usually, we just have to parse it, and we don't need to construct such a parameter.

However, when writing code today, you have to construct such a parameter. The reason is that a third-party module is used (accurately based on a third-party module), and the third-party module needs to accept (int argc , char ** &argv) such parameters when initializing. Notice the reference symbol in it!

Generally speaking, argc argv It is no problem to pass the main function directly. This is true, and passing the main function accepts argv no compilation errors . However, some pits are, this module own parameter resolution is too arbitrary! is not allowed to have redundant parameter items! Once an extra parameter entry causes it to stop parsing the parameter, it eventually causes its parsing to fail. However, in addition to my program to pass parameters to this module, but also need additional parameters Ah!

There's no way I'm going to have to build my own. Only the parameters needed for the third-party module are included argv , but the dog's blood has come up!

How to construct is not possible!! Compile always error:

用类型为‘char**’的右值初始化类型为‘char**&’的非常量引用无效

After a series of changes, and finally compiled correctly.

All the dog blood is because the concept of understanding is not clear.

Solution Solutions

Only argv constructs to consider:

int fack_main(intchar **&argv);int2 ;char"program name" ;char"arg1 for thirdparth" ;charnewchar *[fack_argc+1delete [] fack_argv ;

The above is the complete construction process. Such constructs fack_argv should be main argv completely equivalent to those in the function.

Resolution process

The resolution process is very bitter.

  1. Raw

    fack_argv[2][100] { "program name" , "arg1"  } ;fack_main(fack_

    Error:

    用类型为‘char (*)[100]’的右值初始化类型为‘char**&’的非常量引用无效

    It's a good idea to see this error. Indicates whether the parameter is passed fack_argv or is a char[100] pointer to a type char (*)[100] , not char **& .

    Think of the solution isstatic_cat

  2. Static_cast

    fack_argv[2][100"program name""arg1"static_cast<char

    Result Error:

    从类型‘char [3][100]’到类型‘charstatic_cast 无效

    Why does the cast fail?

    Is it only a step-by-step conversion?

    Try the following code:

    static_cast<char **>(static_cast<char(*)[100

    Want to complete in two steps, but still error:

    错误:从类型‘char (*)[100]’到类型‘charstatic_cast 无效

    This means that the one-dimensional array to the pointer is OK, but the second step fails. In the second step, the code is actually converting one to 指向数组的指针 one 指向指针的指针 . This is probably not supported by the compiler.

    After the failure, again, since the direct declaration of the two-dimensional array failed, it appears that only a one-dimensional array is declared, the array char * is ready to put.

  3. Array pointing to pointer (char*)

     char  arg0[] =  "program name" ; char  arg1[] = " arg1 "; char  *fake_argv[2 ] = {arg0, arg1};  

    The result is still an error:

     error: Use type ' char  * * ' for rvalue initialization type ' Span class= "Hljs-keyword" >char  **& ' A very bad reference is invalid  

    This time you need to be aware that the previous is right-we have constructed char** , but the target is char **& . However, it is strange that the argv of the main will not error, which means that there is still a difference between the two! The

    needs to be changed! The

    Looks at the back section this time--"very good reference"!

    Does it say char * * can be converted to constant reference ?

  4. Test: will be converted to a char *[] constant reference

    After a long day of tinkering, I finally figured out how a constant reference is declared:

    char *fake_argv[2] = { arg0 , arg1  } ;charconst (&ra) = fake_argv ;

    Pass!

    As can be seen from the above, the so-called is to require a low-level const, that is, 常量引用 ra the value can not be changed.

    This test is not very helpful for the problem, but it is implicitly stated--or the variable type declares a problem.

    Search online!

  5. Online Searchargv *[]

    View defining own main functions arguments argc and argv

    The problem is found to be similar, but the solution provided still does not solve car **& the problem of formal parameters.

    But the harvest:argv finally a more null-closing flag .

  6. Exactly the same type!

    Since it's char ** a char **& mistake, my arguments are still arrays, which means the compiler has implicitly converted.

    I remember saying in the constructor of the class that the implicit conversion of a class is at most once.

    Is there such a possibility:

    A value parameter to a reference parameter is actually an implicit conversion ?

    Plus the previous guess-at most one implicit conversion-the problem is clear. The one-time implicit conversion is used to change char *argv[] char **argv , so it causes a char ** char **& failure.

    The book is not read much, I think there should be a relevant introduction. This is just speculation.

    In this way, there is a final solution:

    int fack_main(intchar **&argv);int2 ;char"program name" ;char"arg1 for thirdparth" ;charnewchar *[fack_argc+1delete [] fack_argv ;

    by compiling! Problem solving.

Manually construct parameters that are equivalent to ' char *argv[] '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.