Question:
annoying C++ gcc warning message?
?
2013-06-26 10:06:41 UTC
I've written the following program to match regular expressions in C++

#include
#include

using namespace std;

/*
* Match string against the extended regular expression in
* pattern, treating errors as no match.
*
* return true for match, false for no match
*/

bool match(const char *string, char *pattern)
{
int status;
regex_t re;

if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0)
return false; /* report error */

status = regexec(&re, string, (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
return false; /* report error */
}
return true;
}

int main()
{
string str = "def fadi 100";
bool matchExp = match(str.c_str(), "^[Dd][Ee][Ff][' '\t]+[A-z]+([,])?[' '\t]+[0-9]+$");
cout << (matchExp == true ? "Match": "No match") << endl;
}


The program works fine just as expected, but when I compile the code using gcc with the -Wall -Werror arguments (Linux environment), I get a very annoying warning message saying the following:
main.cpp: In function ‘int main()’:
main.cpp:33:90: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

Is there a way to force the compiler to believe that str.c_str() is the same as char * str? if so, how?
Four answers:
?
2013-06-26 18:37:53 UTC
Just change the prototype from

bool match(const char *string, char *pattern)

to

bool match(const char *string, const char *pattern)



You are passing a constant, so declare/define a constant ...
Fred W
2013-06-28 00:10:43 UTC
String constants are not writeable. The one being complained about is the pattern.



Changing the declaration



bool match(const char *string, char *pattern)



to



bool match(const char *string, const char *pattern)



takes care of your warning:



with your declaration:



[fred@dejah ~]$ g++ -c rg.c++

rg.c++: In function ‘int main()’:

rg.c++:32:86: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

[fred@dejah ~]$



and with the fix:



[fred@dejah ~]$ g++ -c rg.c++

[fred@dejah ~]$



See? it's actually NOT the str.c_str() in this case. But, in case you were wondering, c_str() ALSO needs const char *, because it doesn't return writeable storage either. In this case, you had that declaration correct.



The difference is that passing a string constant to a "char *" declaration is only a warning, whereas passing c_str() to a "char *" is an error.
2013-06-26 20:36:55 UTC
I agree with above. It looks like it is complaining that you have sent a String where it is expecting a char*. Deprecated is a bit surprising, as I would expect gcc to do the conversion in newer versions. Try creating a char* and use it in place of your character list. A good idea in either case as it is so unwieldy.
jplatt39
2013-06-26 17:34:32 UTC
No. Actually it looks like the > "^[Dd][Ee][Ff][' '\t]+[A-z]+([,])?[' '\t]+[0-9]+$" is what it's complaining about. If it compiles, live with it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...