-2

So I'm just trying to write an empty string to a char [80] and I get the error "cannot convert string to char* in getstr()"? (I get this in general when writing a string to a char array like char[80] = "hello world".)

This includes a lot of ncurses stuff btw. Didn't include other functions.

int main(){
    std::string message;

    //...

    for(;;){
        wrefresh(chatWin);
        message = "hello world";

        getstr(message);
        if(message != ""){
            displayMessage(message, displayLine, displayWin);
            wmove(chatWin, 1, 7);
        }
    }
}

I expect it to get the input from the user from the function but it throws the error "cannot convert string to char* in getstr()". getstr is from ncurses. Any help is greatly appreciated and thank you in advance!

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Versalys
  • 55
  • 8
  • Don't use `char message[80];`, instead use `std::string message;`. – Eljay Jul 27 '19 at 17:58
  • 7
    You cannot assign a string literal to a `char` array. C++ does not work this way. If your intent is to write modern C++ code, use `std::string`. If your intent is to write old-style C code, use `strcpy()`. – Sam Varshavchik Jul 27 '19 at 18:00
  • @Versalys show us how you tried to use `std::string`. – Fureeish Jul 27 '19 at 18:04
  • To set `message` to an empty string do `message[0] = 0;` – Wyck Jul 27 '19 at 18:06
  • sorry, when I was trying to edit my code I created a string array lol. fixed that. But now i run into the error with getstr(message) because it cant convert the string to a char*. – Versalys Jul 27 '19 at 18:10
  • 2
    By changing your question to use `std::string message` instead of `char message[80]`, now you have a completely new question. Your `getstr` is expecting a `char*`, not a `std::string`. Although `std::string` is a excellent idea, it's a bigger change to make use of it; you can't just drop-in replace the declaration of `message` and not adjust all the usages. You should probably put it back, lest this question get derailed. – Wyck Jul 27 '19 at 18:20
  • @Wyck is there anyway to cast a string to a char* for the function or not? – Versalys Jul 27 '19 at 18:24
  • @Versalys See my answer, and [this one](https://stackoverflow.com/questions/4037474/is-there-any-way-to-pass-a-stdstring-to-a-function-that-accepts-a-char-and-ch) – Wyck Jul 27 '19 at 18:29
  • Another effect of changing your question's code to use `std::string` instead of `char [80]` is that the text of your question no longer makes sense when it mentions `char [80]`. In its current hybrid form (even before my edit), the question is difficult to understand. – JaMiT Jul 27 '19 at 20:28

1 Answers1

0

I suspect you had written something like this:

int main() {
   char message[80];
   message = "";
}

This will yield the error:

error: incompatible types in assignment of 'const char [1]' to 'char [80]'

To set message to an empty string you can do it the idiomatic way of copying an empty string into the message array like this:

strcpy(message, "");

or simply place a null terminating character at the beginning of the array like this:

message[0] = 0;

Can't, with good conscience, write this post without mentioning that std::string is a good idea. But it may be inconvenient to port all your existing code that uses a char array to the technique of using a std::string. For example, getstr likely expects a char* not a std::string. Instead you'll need to interoperate between char arrays and std::string by way of a std::vector<char>. I think it's out of scope to answer exactly how in this answer.

#include <string>
#include <curses.h>

int main() {
   std::string message;
   message = ""; // This works.
   getstr(message); // This doesn't. (assuming `getstr` from curses.h)
}

See also Is there any way to pass a std::string to a function that accepts a char* and changes its contents?

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • Never use `strcpy` or similar functions when you don't know the size of source strings; it can lead to undefined behaviour and security vulnerabilities. Use `strncpy` (and `getnstr`) instead. – Erbureth Jul 27 '19 at 18:31
  • By the way, since C++17 the underlying buffer accessed by `std::string::data()` is no longer `const`, so you can use it directly. – Erbureth Jul 27 '19 at 18:37
  • @Erbureth, your advice to write the `getnstr` data `to std::string::data()` without knowing the destination size is also dangerous. You didn't explain how to ensure there are enough bytes at `data()` for the `getnstr` to be successful (presumably at least 80 are desired). You didn't explain how `getnstr` would set the length of the `std::string` if it writes fewer chars than the original size of the string. Something ugly like `message.erase(std::find(message.begin(), message.end(), '\0'), message.end());` after the `getnstr` would be required I think. I don't recommend your advice as written. – Wyck Jul 27 '19 at 18:55
  • This is not the answer, but comment to your answer. – Erbureth Jul 27 '19 at 18:56
  • Thanks @Wyck for helping me with my problem, even if I didn't know what in the holy hell I was doing, I got it running after a couple hours. It just cost me my stackoverflow reputation XD – Versalys Jul 27 '19 at 19:22
  • You can use `std::string::resize()` to tell a given string how many characters it should have, @Wyck. In this case, if used with `getnstr()`, you would want it to have a size of at least `n`. – Justin Time - Reinstate Monica Jul 27 '19 at 21:56