1

I have an old C/C++ package which I am trying to compile with CygWin because it needs Motif and other X- things. Most of it compiles OK but there are some warnings due to lines like....

static String fallbackResources[] = { "Joe", ..etc.. , NULL};

I get the compiler warning: deprecated conversion from string constant to ‘String {aka char*}’

I have googled and found many suggestions to avoid this warning by changing occurrences of say "char* fred[]" to "const char* fred[]" which I have done for most of the c++ files in the package and this has worked perfectly to remove the compiler warnings.

However I am stuck with the "static String" lines since when I change them by inserting "const" before "String" it makes no difference and if I change the "String" to "const char*" the warning disappears but the program doesn't compile due to an error later on where it sends the array to another function....

cannot convert ‘const char*’ to ‘char**’ for argument ‘7’ to....

Any help would be very much appreciated.

bat
  • 21
  • 4
  • When you changed the declaration to "const char*" you didn't remove the '[]'? You need those. – Dweeberly Dec 13 '13 at 22:49
  • the String type, is it a std::string? If not where does it originate from? and are you sure it is exactly on that initialization that things go berserk? – Steve Van Opstal Dec 13 '13 at 22:56
  • Can you post the header of the other function? Sounds like this static array isn't supposed to be an array of constant strings but rather an array that contain regular char*'s as well. – makhdumi Dec 13 '13 at 23:04
  • Edit took to long: Can't you just leave it as is and compile with the warning? It sounds like a necessary evil. If you can't do with the warning, then leave the change as "static const char* fallbackResources[]" and when passing to the other function, copy the static array into an actual char** array. – makhdumi Dec 13 '13 at 23:11
  • Thanks all. I did leave the [] in and I can't see any definition of String in the files. My aim is to get rid of the errors. OK I can suppress the warnings but I would like to remove them first. Here's where it goes wrong....mainscreen.c++: In function ‘int main(int, char**)’: mainscreen.c++:69:53: error: invalid conversion from ‘const char**’ to ‘char**’ [-fpermissive] NULL, 0, &argc, argv,fallbackResources, NULL); ^ In file included from /usr/include/Xm/Xm.h:59:0,... – bat Dec 13 '13 at 23:44

2 Answers2

1

Thanks all, googled more and found (https://stackoverflow.com/a/14648630/3100869) - the answer seems to be to use const_cast's like this:

static String fallbackResources[] = { const_cast("Joe"), ..etc.. , NULL};

... and the warnings go away!

Community
  • 1
  • 1
bat
  • 21
  • 4
0

The problem is that you've got two levels of const on pointers: the pointer itself and what it points to. char const * is a pointer to const char (read from right to left), whereas char* const is a const pointer to char.

It seems that String in your case is a typedef for char*. Making that const like you did turns it into a char* const. You need to fix the typedef.

MSalters
  • 173,980
  • 10
  • 155
  • 350