Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’
I was looking into strings in C++ and tried an exercise to experiment the behaviour of some of the functions defined in the string library. I compiled the same program yesterday and everything worked with absolutely no warnings or errors. However, today I tried to compile the program again, but I received the following warning.
D:\C++ CodeBlocks Ex\Strings\main.cpp||In function 'int main()':|
D:\C++ CodeBlocks Ex\Strings\main.cpp|11|warning: deprecated conversion from string constant to 'char*'|
||=== Build finished: 0 errors, 1 warnings ===|
the warning referes to this line strncat("Hello",string,STRMAX-strlen(string));. I am not sure but from what I suspect is that the strncat function does not like the ideaa of having to concatenate an array of literals with a string constant. Any help on this will be much appreciated.
#include <iostream>
#include <string.h>
using namespace std;
int main(){
#define STRMAX 599
char string[STRMAX+1];
cout<<"Enter name: ";
cin.getline(string,STRMAX);
strncat("Hello",string,STRMAX-strlen(string));
cout<<string;
return 0;
}
You’re supplying the arguments to
strncat()in the wrong order. The first argument is the string to append to; the second argument is the string to append. As written, you’re trying to add the inputstringto the constant string"Hello", which isn’t okay. You’ll need to write this as two separate string operations.Using the
std::stringclass will save you a lot of grief.