2

I get "passing argument 2 of ‘execvp’ from incompatible pointer type" and
expected ‘char * const*’ but argument is of type ‘const char **’
I'm wondering what the correct syntax is? Thanks!


int main(int argc, const char* argv[]) {
  if(argv[0]!=NULL)
    return -1;
  int pid = fork();
  if(pid==0)
    execvp(argv[0],argv+strlen(argv[0]));
  else
    wait();
  return 0;
}
Josh
  • 142
  • 2
  • 11

1 Answers1

2

exec functions don't accept const char*. In your case, simply change argv to char*, that's the correct prototype.

Btw. argv + strlen(argv[0]) doesn't make any sense, what did you mean by that?

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • Great! Just deleting const seems to have fixed it! However, if I run it with say emacs, it doesn't actually load it. – Josh Apr 27 '11 at 19:10
  • Actually, the `execl` family does accept `char const*`, but the `execv` variants don't. @Josh: `const char* argv[]` isn't standard C anyway. – Fred Foo Apr 27 '11 at 19:11
  • @Let_Me_Be: yes it is. They're exactly the same. – Fred Foo Apr 27 '11 at 19:12
  • 2
    @Josh Most likely because `argv + strlen(argv[0])` doesn't make any sense did you mean `argv+1`? – Šimon Tóth Apr 27 '11 at 19:13
  • @Let_Me_Be I think it's supposed to be execvp(argv[1],argv+2); since argv[0] is ./a.out and arv[1] is emacs and the rest are any args given, but that doesn't work either – Josh Apr 27 '11 at 19:16
  • @Let_Me_Be what does argv+1 actually mean? Is it passing argv[1] through arv[n]? – Josh Apr 27 '11 at 21:31
  • 1
    @Josh `argv+1` is `&argv[1]`. Because `a[b]` is defined as `*(a+b)` – Šimon Tóth Apr 28 '11 at 14:25