1

I'm not exactly sure how to accomplish this, I have searched and tried many different things but I just cant get it to work? strcpy(host, DATA); is what gives me the error.

char host[60] = "www.yahoo.com"; void loop() { String content = ""; char character; while (Serial.available()) { character = Serial.read(); content.concat(character); } if (content != "") { String CMD = getValue(content, '|', 0); String DATA = getValue(content, '|', 1); if (CMD == "SSID") { Serial.println("Your CMD is:" + CMD + " And your DATA is:" + DATA); } else if (CMD == "PASS") { Serial.println("Your CMD is:" + CMD + " And your DATA is:" + DATA); } else if (CMD == "HOST") { Serial.println("Your CMD is:" + CMD + " And your DATA is:" + DATA); strcpy(host, DATA); } } delay(100); } 
4
  • 1
    Is String a std::string? a custom class? Commented Feb 5, 2016 at 0:12
  • What is String? It's certainly not a char*, which is what strcpy expects. You need to convert String to a const char*, possibly through its member function. For example, std::string has a c_str() member function that does that. Commented Feb 5, 2016 at 0:12
  • I just dont understand what i can do strcpy(host, "whatever"); but I cant do strcpy(host, DATA); Commented Feb 5, 2016 at 0:16
  • @JohnBrodowski Because C++ is strongly typed, and the type of DATA is String, which is not the same as char * or const char*, and also there is no implicit conversion from String to char* or const char*. Commented Feb 5, 2016 at 0:18

1 Answer 1

5

If String, is a std::string, you can do

strcpy(host, DATA.c_str()); 

If it's a custom class for example

class String { char buffer[20]; //add a method c_str() for example like the std::string does const char * c_str() { return buffer; } //or just go yolo and overload the operator const char * like a boss operator const char *() { return buffer; } }; 

and all you have to do now is strcpy(buff, DATA) and the operator const char * () will be used.

Sign up to request clarification or add additional context in comments.

5 Comments

I'd be careful with implicit conversion operators, as they may kick in whenever you will expect the least. In C++11 you can make them explicit, or just use the c_str() member function.
True, forgot about that.
Now you need a cast in strcpy(buff, (const char*)DATA) because it won't convert implicitly ;)
@vsoftco not really, because strcpy takes a a const char* as the second parameter.
Then a cast to const char*, sorry. But definitely you need to explicitly cast if your conversion operator is explicit, see coliru.stacked-crooked.com/a/5b48049baac0e5b0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.