添加到 c* 从 const char* 到 char* 的无效转换"; [英] Adding to c* invalid conversion from const char* to char*"

查看:37
本文介绍了添加到 c* 从 const char* 到 char* 的无效转换";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 WiServer.h 的 Arduino 文件中具有以下功能.

I have the function below in a file called WiServer.h for Arduino.

GETrequest(uint8* ipAddr, int port, char* hostName, char* URL);

现在的问题是我需要将一个 int 值 (setting1) 连接到 char* URL 参数,例如如下所示.

Now the problem is I need to concatenate an int value (setting1) to the char* URL parameter like the below for example.

"twittermood.php?status=sendTweet&setting1="+setting1

我收到一个错误:

从 const char* 到 char* 的无效转换

invalid conversion from const char* to char*

我该如何解决?

推荐答案

您已经获得了不错的通用 C++ 建议,但是对于 Arduino 在 8 位 AVR 微控制器上的特殊情况,我认为您需要特定于平台的建议:

You've gotten decent generic C++ advice, but for the special case of Arduino on an 8-bit AVR microcontroller I think you need platform-specific advice:

Arduino 运行时提供了一个 String 对象.这些示例可能涵盖了您的问题.

The Arduino runtime provides a String object. Your question is probably covered by these examples.

由于 Arduino 上的 RAM 空间非常有限,因此通常使用特殊属性将常量字符串放入需要不同指令才能访问的闪存(本质上是 ROM)中.使用 GCC 构建的 AVR 代码通常构建在 AVR Libc 之上,它支持操作在常量字符串和 RAM 字符串的混合上.您必须在代码中明确并选择正确的操作.这至少需要对 C 字符串的工作原理和指针的工作原理有基本的了解.我不确定 Arduino 字符串自动提供了多少这种聪明,但如果没有这种聪明,所有字符串常量最终都会在启动时复制到 RAM 中,并一直占用这些宝贵的字节.

Because of the very limited RAM space on Arduino it is common to use special attributes to put constant strings in flash (essentially ROM) which requires different instructions to access. AVR code built with GCC is typically built on top of AVR Libc which has support for operating on a mix of constant strings and RAM strings. You must be explicit in your code and choose the right operations. This requires at least a basic understanding of how C strings work and how pointers work. I'm not sure how much of this cleverness is automatically provided by the Arduino String, but without this cleverness all of your string constants will end up copied into RAM at boot and will take up those precious bytes all the time.

如果 RAM 空间成为问题,或者您正在开发一个执行大量字符串操作的 AVR 应用程序,您需要学习如何使用 PGM Space 操作(可以在 flash 中处理只读字符串的字符串函数)和 常规的 C 风格的基于 RAM 的字符串操作.

If RAM space becomes a problem or you are working on an AVR application that does extensive string manipulation you need to learn how to use the mix of PGM Space operations (string functions that can work on read-only strings in flash) and regular C-style RAM-based string operations.

这篇关于添加到 c* 从 const char* 到 char* 的无效转换";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆