我正在尝试将const char*
变量放入带有连接的字符串中,这些只是初始化变量。这是我尝试在以下内容中执行此操作的方法String input
:
const char* topic1 = "home/bathroomlight";
const char* topic2 = "home/bathroomfan";
const char* topic3 = "home/dressingroomlight";
const char* topic4 = "home/makeuplight";
const char* topic1_status = "home/bathroomlight/status";
const char* topic2_status = "home/bathroomfan/status";
const char* topic3_status = "home/dressingroomlight/status";
const char* topic4_status = "home/makeuplight/status";
String input = "{ \"Bath Room Light\" : { \"pin\" : 1, \"status\" : \"off\", \"type\" : \"light\", \"command_topic\" : "+ topic1 +", \"state_topic\" : "+topic1_status +" }, \"Bathroom Fan\" : { \"pin\" : 2, \"status\" : \"off\", \"type\" : \"fan\", \"command_topic\" : "+topic2+", \"state_topic\" : "+topic2_status+" }, \"Dressing Room Light\" : { \"pin\" : 4, \"status\" : \"off\", \"type\" : \"light\", , \"command_topic\" : "+ topic3 +", \"state_topic\" : "+ topic3_status +" }, \"Makeup Light\" : { \"pin\" : 3, \"status\" : \"off\", \"type\" : \"light\", \"command_topic\" : "+ topic4 +", \"state_topic\" : "+ topic4_status +" } }";
当我尝试将这些值附加到input
String 时。它不允许我。我猜上面的值是const char*
如何添加的?任何建议都会有所帮助。
问题是本机字符串(空终止字符数组)和通过字符指针引用的字符串不使用运算符连接+
。但是Arduino
图书馆的字符串类String
确实如此。
因此,首先您必须构造一个String
类对象,然后将您的字符数组连接到该对象:
const char* s1 = "hello";
const char* s2 = "world";
String input = String() + s1 + " " + s2;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句