On Wednesday, 5 October 2022 at 23:40:12 UTC+1 roger wrote:
string s1 = to_string(z);
string s2 = s1.c_str();
// win2_unitprice->value(s1);
win2_unitprice->value(s1.c_str());
If I uncomment line 3 I get the following error:-
error: no matching function for call to ‘Fl_Input::value(std::__cxx11::string&)’
win2_unitprice->value(s1);
but if i look at the data I see that:-
s1 holds the address of the data 0x31 0x2e 0x31 0x30 ....
and
s2 holds the address of the data 0x31 0x2e 0x31 0x30 ....
so why do I need to use c_str() to make this work ??
Not at all clear what you think s2 is doing here? You declare and set it but do not appear to use it?
In any case, the "answer" to your question is that Fl_Input::value() does not take a "string" in the C++ sense, it takes a pointer to an array of characters; which is often referred to as a "string" in the C-sense, but is a different animal.
The fltk API was (in large part, anyway) originally inherited form a previous, proprietary, lib called "Forms" which was a C lib, and presented a C API. In consequence, much of fltk also presents a fairly C-like API. Which often aids portability, but may seem archaic now...
A C++ "string" is an object which holds a string of text; the actual content is notionally opaque, but it probably does hold an array of characters, along with various attributes about the string, such as length and so on, and methods to manipulate that data.
A C "string" is just a sequence of bytes that ends in a zero - you don't even know how long it is unless you count 'em.
The C++ object is a much less error-prone entity, but that's a whole other story.
Welcome to the world of C-programming...