c++arrayscconstantsexecv

Creating the execv argument array when the arguments are unknown at compile time


I am trying to revive an in-house CAM application, i.e. get it to compile on a current gcc so I can maintain it. I have 27 years of experience as an occasional C/C++ hack ;-)

The part in question is the command-line core, so no UI. It has a sort of front-end that does some checking and manipulation of its input arguments, and moves them into a new argv, used by execv to switch to 1 of the 2 different executables where the real work is done.

The existing code accounts for 5 different CL options, although from what I've seen only 2 are normally used. When trying to populate the new argv (char* const new_argv[32]) any way other than with a brace-enclosed list in the declaration, the compiler says I can only do it with a brace-enclosed....

I've googled for 2 days with no compelling results. Is there a slick way to do this, or just the ugly brute force way? Is there something seriously wrong with the whole design?


Solution

  • Change the type of the array to char *[], then you can assign to each array member before passing it to execv.

    This is allowed because a char *[] can be converted to a char * const[] but not a const char *[] or const char * const[]