在字符串中加入 const char*

用户3201500

我正在尝试将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 +" } }";

当我尝试将这些值附加到inputString 时。它不允许我。我猜上面的值是const char*如何添加的?任何建议都会有所帮助。

加利克

问题是本机字符串(空终止字符数组)和通过字符指针引用的字符串使用运算符连接+但是Arduino图书馆的字符串类String确实如此。

因此,首先您必须构造一个String类对象,然后您的字符数组连接到该对象:

const char* s1 = "hello";
const char* s2 = "world";

String input = String() + s1 + " " + s2;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

const char *与字符串文字的使用

const char *是字符串还是指针

如何比較 const char* 字符串?

C ++-char const * const *-<读取字符串的错误>

为什么要使用const char *形式的字符串

从C ++中的字符串文字返回const char *吗?

从int转换为c字符串(const char *)失败

在存储在const char数组中之前修改字符串

从const char *转换为Swift字符串

const volatile char字符串无法正确打印

创建字符串如何改变const char *指向的值?

从Swift字符串转换为const char *

将字符串转换为const * char

字符串文字vs const char *函数重载

C ++-从const char *复制到字符串

错误:无法将参数1的const字符串转换为const char *到size_t strlen(const char *)

(const char *)++或(const char)++?

由c ++字符串类定义的字符串文字是否为const char []类型

无法将 const char 转换为字符串构造字符串

由c ++字符串类定义的字符串文字是否为const char []类型

从 C# 传递的字符串与 C++ 字符串不同,都是 const char

字符串参数 const char* 和 const wchar_t*

一次将字符串文字常量定义为char const *和wchar const *

C /使用动态malloc复制字符串,从const char * org到char ** cpy

Ruby FFI接口从字符串转换为const char *时的C ++字符编码

const char * vs const char []

qFatal参数:将QString转换为const char *会导致警告“格式字符串不是字符串文字”

const char **和char **

如何将C ++ / CLI字符串转换为const char *