4

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Pietro
  • 12,086
  • 26
  • 100
  • 193

6 Answers6

4

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));
}
Pietro
  • 12,086
  • 26
  • 100
  • 193
1

You are converting a string constant to a mutable character pointer, change using an implicit cast, the compiler is warning you that newer versions of the language will not allow this cast.

When defining a string litteral c++ understands it to mean a constant character array you have defined it as a mutable character array, change your code accordingly.

awiebe
  • 3,758
  • 4
  • 22
  • 33
1

Change:

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

To:

char *const args[] = {"/bin/ls", "-r", "-t", "-l", NULL };
Jonathan
  • 859
  • 7
  • 15
1

The only reason you get the warning is that you're using g++, not gcc. In pure C, you'd get absolutely no warnings. It's actually quite difficult to create warning-free C++ code from this. To be honest, I tried but did not succeed.

These hurdles are one of the reasons for the existence of a certain philosophical school. See more here.

SzG
  • 12,333
  • 4
  • 28
  • 41
  • After reading that comment by Linus Torvalds on C++, I would like to assist to a match between Torvalds and Stroustrup. I'm sure there would be a lot of blood :-) For what I am concerned, I would never go back from std::vector to malloc! – Pietro Sep 05 '13 at 14:16
1

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 <unistd.h>
main()
{
    char* const args[] = {(char*)"/bin/ls", (char*)"-r", (char*)"-t", (char*)"-l", (char*) 0 };
    execv("/bin/ls", args);
}

OR

#include <unistd.h>
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

Ethan
  • 1,206
  • 3
  • 21
  • 39
  • The important difference is `char const*` vs `char * const`! First one is a pointer to *constant char*, second is a *constant pointer* to char. See also [Constness](https://en.cppreference.com/w/cpp/language/pointer#Constness) – Olaf Dietsche Feb 24 '20 at 10:43
0

change

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

to

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

for usability u may will make a simple convert method.

guest
  • 1