-2

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;
}
Community
  • 1
  • 1
Alex Goja
  • 548
  • 5
  • 18

4 Answers4

5

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 input string to the constant string "Hello", which isn't okay. You'll need to write this as two separate string operations.

Using the std::string class will save you a lot of grief.

4

Since you're using C++, I would recommend avoiding char* and using std::string instead. If you need to pass in a char*, the string class has a c_str() method that returns the string in the form of a const char*.

Concatenation while using the string class is as easy as "Hello " + "World!".

#include <iostream>
#include <string>

const int MaxLength = 599;

int main() {
    std::string name;

    std::cout << "Enter a name: ";
    std::cin >> name;

    if (name.length() > MaxLength) {
        name = name.substr(0, MaxLength);
    }

    // These will do the same thing.
    // std::cout << "Hello " + name << endl;
    std::cout << "Hello " << name << endl;

    return 0;
}

This doesn't fully answer your question, but I guess it might help.

user123
  • 8,970
  • 2
  • 31
  • 52
3

A better way of writing this is with the string class and skip char completely.

#include <iostream>
#include <string>

int main(){
    std::string name;
    std::cout<<"Enter name: ";
    std::getline(std::cin, name);
    std::string welcomeMessage = "Hello " + name;
    std::cout<< welcomeMessage;
    // or just use:
    //std::cout << "Hello " << name;
    return 0;
}
default
  • 11,485
  • 9
  • 66
  • 102
1
char * strncat(char * destination, const char * source, size_t num);

So your source and destination are the wrong way around.

Neil
  • 54,642
  • 8
  • 60
  • 72