Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use execv() without warnings?

I am working on MacOS-X Lion with GCC 4.2. This code works, but I get a warning I would like fix:

#include <unistd.h>
main()
{
    char *args[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 };
    execv("/bin/ls", args);
}

warning: deprecated conversion from string constant to 'char*'

I do not want the warning to be suppressed, I want not to have it at all. It is C++ code, not C.

Using a char *const (so exactly the type required by execv()) still produces the warning.

Thank you.

like image 792
Pietro Avatar asked Feb 21 '23 08:02

Pietro


1 Answers

This seems to be ok:

#include <unistd.h>
main()
{
    char const *args[] = {"/bin/ls", "-r", "-t", "-l", NULL };
    execv("/bin/ls", const_cast<char**>(args));
}
like image 70
Pietro Avatar answered Feb 27 '23 03:02

Pietro