0

Hello everyone i'm a newbie to system programming,please mind me if my doubt is very vague.

I'm actually following a book named Linux System Programming and I'm having a doubt in execvp() system call.As given by a book example I tried it on my machine and here is the following example ..

#include<unistd.h>

//int ret;
int main(){

const char *args[] = { "vi", "/home/kidd/hooks.txt", NULL };
int ret;
ret = execv ("/bin/vi", args);
if (ret == −1)
perror ("execvp");

}

And i'm receiving a foolwing error:

error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]

I've given a const char arrays name which is obviously const char**.

Why is it giving this error?**

jack wilson
  • 145
  • 1
  • 13

1 Answers1

0

The args array should not have type const char*, it should just be char* (in the same way that the argv argument to main is just a char*). So your code should look like this:

#include <unistd.h>

int main() {
 char *args[] = { "vi", "/home/kidd/hooks.txt", NULL };
 int ret;
 ret = execv ("/bin/vi", args);
 if (ret == -1)
    perror ("execvp");
}
imareaver
  • 211
  • 2
  • 5
  • But I didnot understand the logic.The argument type should be const char** how did the string literals inside affect the type.I'm a c++ programmer mind me if errors. – jack wilson Dec 24 '14 at 18:29
  • The type you want is const* char*. Since the literals have type const char*, declaring the array type const char* gives you type const char**. So instead you make the array a char* to get type const* char*. http://linux.die.net/man/3/execvp http://stackoverflow.com/questions/12517983/c-what-is-the-datatype-of-string-literal – imareaver Dec 24 '14 at 19:11