0

I am trying to call execv() with some arguments, imagePath and outputPath are std::string so I converted those to C strings, then I am converting integers to C strings as well.

The error I'm getting is

function not viable: no known conversion from 'const char *[6]' to 'char *const *' for 2nd argument
int      execv(const char * __path, char * const * __argv) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;

What is the difference between const char *[] and char *const *?

void Utility::ExecCrop()
{
    // convert numbers to strings
    const char* x_char = std::to_string(job.x).c_str();
    const char* y_char = std::to_string(job.y).c_str();
    const char* w_char = std::to_string(job.w).c_str();
    const char* h_char = std::to_string(job.h).c_str();
    
    const char* arguments[] = { imagePath.c_str(), outputPath.c_str(), x_char, y_char, w_char, h_char };

    execv(execPath.c_str(), arguments);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Adam Jean-Laurent
  • 205
  • 1
  • 2
  • 8
  • 2
    Just as a side note: The list of `arguments` must be terminated by a `NULL` pointer (or `nullptr` in C++). See the [documentation on the function `execv`](https://linux.die.net/man/3/execv) for further information. – Andreas Wenzel Oct 31 '20 at 22:12
  • 2
    Another side note: How long does temporary values exit in c++? Wouldn't the temporary `std::string` returned by `std::to_string` evaporate after `.c_str` has been called, and take the buffer with it? Better to bind the string to a variable: `std::string s = std::to_string(foo); const char *cs = s.c_str();` – HAL9000 Oct 31 '20 at 22:47
  • 1
    More side notes: By convention, the first argument to an executable should be the name of the execuatable. – HAL9000 Oct 31 '20 at 22:50

0 Answers0