How to use execv() without warnings?

前端 未结 6 1856
半阙折子戏
半阙折子戏 2021-01-03 07:46

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

#include 
main()
{
    char *args[] = {\"/b         


        
6条回答
  •  清歌不尽
    2021-01-03 08:37

    I do not know why the accepted answer was selected, it does not remove any warnings when I run this code....

    I cannot confirm on your specific platform, but adding casts to each string constant has made the warnings go away for me.

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

    OR

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

    It may be overly verbose and annoying, but the warnings go away.

    I'm running this on: g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

提交回复
热议问题