execv() and const-ness

后端 未结 4 1008
时光说笑
时光说笑 2020-12-05 06:59

I often use the execv() function in C++, but if some of the arguments are in C++ strings, it annoys me that I cannot do this:

const char *args[4]         


        
4条回答
  •  没有蜡笔的小新
    2020-12-05 07:32

    const is a C++ thing - execv has taken char * arguments since before C++ existed.

    You can use const_cast instead of copying, because execv doesn't actually modify its arguments. You might consider writing a wrapper to save yourself the typing.

    Actually, a bigger problem with your code is that you declared an array of characters instead of an array of strings.

    Try: const char* args[4];

提交回复
热议问题