1

I want to use the execev function to run the texwork program from a fork of another program, and therefore, i have the following setup :

char *argVec[3];
argVec[0] = "texworks";
argVec[1] = "temp.tex";
argVec[2] = NULL;
execvp("texworks", argVec);

it works, but warns me :

Warnung: veraltete Konvertierung von Zeichenkettenkonstante in »char*« [-Wwrite-strings]
  argVec[1] = "temp.tex";

that is : warning, old conversion from string constant to char* (the same warnign for argVec[0])

Should I be worried, and if so, how to i avoid this?

(oh, i am in Linux, 64 bit, g++ 4.8.1 -2013 prerelease, and const char* argVec[] = {"texworks" .. etc failes with this :

Fehler: ungültige Umwandlung von »const char**« in »char* const*« [-fpermissive]
  execvp("texworks", argVec);
                           ^
In file included from path/to/file:
/usr/include/unistd.h:578:12: Fehler:   Argument 2 von »int execvp(const char*, char* const*)« wird initialisiert [-fpermissive]
 extern int execvp (const char *__file, char *const __argv[])
        ^
/path/to/file:cursor:position: Fehler: Sprung zur case-Marke [-fpermissive]
  default:
  ^
/path/to/file:cursor:position:: Fehler:   überschneidet Initialisierung von »const char* argVec [3]«
  const char * argVec[] = {"texworks" , "temp.tex", NULL};
Sean
  • 789
  • 6
  • 26
  • As it is, you array could be an array of const char*. Maybe that's what is meant because you basically cast a const to a non-const here implicitly. – nvoigt Nov 08 '13 at 09:48
  • [`execvp`](http://linux.die.net/man/3/execvp) requests `char *const argv[]` for the second parameter, so perhaps use a decl that is compliant to avoid the warning. – WhozCraig Nov 08 '13 at 09:48
  • See also http://stackoverflow.com/questions/190184/execv-and-const-ness, http://stackoverflow.com/questions/10456043/why-is-argv-parameter-to-execvp-not-const or http://stackoverflow.com/questions/19505360/why-does-execvp-take-a-char-const-argv – gx_ Nov 08 '13 at 10:16

1 Answers1

3

The warning is legitimate, because assigning a "const char*" to a "char * " is dangerous. The data pointed to can be changed, but it shouldn't.

To build the argument vector using const char*, declare the array as a char const * const[]

To pass the array to execv, cast it to char**.

This version should avoid the warning:

char const * const argVec[] = {
  "texworks"
, "temp.tex"
, NULL
};
execvp("texworks", (char**)argVec);
Pat
  • 1,726
  • 11
  • 18
  • 1
    "look at the prototype of the execvp function" [Yes please](http://linux.die.net/man/3/execvp): int execv(const char \*path, **char** \*const argv[]); so that's a shame but execvp won't accept your `char const * const argVec[]` – gx_ Nov 08 '13 at 09:59
  • I see your point. You'll likely need a cast. Will edit. – Pat Nov 08 '13 at 10:06