error: invalid conversion from 'const char*' to 'char*' when trying to use pointer arithmetics
error: invalid conversion from 'const char*' to 'char*' when trying to use pointer arithmetics
我觉得问这个问题很愚蠢,因为解决方案一定很明显。我收到以下错误
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
char *subtopic = topic+strlen(mqtt_meta_ctrl_topic);
对于以下代码:
void HandleIncomingMQTTData(const char* topic, const int topic_len, const char* data, const int data_len)
{
// Determine subtopic
char *subtopic = topic+strlen(mqtt_meta_ctrl_topic);
printf("%.*s", topic_len-strlen(mqtt_meta_ctrl_topic), subtopic);
}
如您所见,我尝试在 topic
-string 中获取一个“视图”,其中 subtopic
的地址仍在 topic-string 中,但位于更下游的地址。我想我的指针算法有点不对劲,但我不知道为什么,因为我没有更改 const char *topic
字符串。
topic
是 const char *
,但是你的 subtopic
是 char*
。
const char *subtopic = topic + whatever;
printf("%.*s", topic_len-strlen(...)
请注意 strlen
returns size_t
,但 .*
需要 int
。你应该在这里做一个转换 printf("%.*s", (int)(topic_len - strlen(...))
.
为了提高性能,使用 fwrite
等而不是 printf
可能更好。
查看下面的代码:
#include <stdio.h>
#include <string.h>
char mqtt_meta_ctrl_topic[100] = "Hello world !";
const char *t = "Have a good day !";
const char *d = "R2D2 & C3P0";
void HandleIncomingMQTTData(const char* topic, const int topic_len, \
const char* data, const int data_len)
{
printf("topic = '%s', topic_len = %d\n", topic, topic_len);
printf("topic = %ld\n", (unsigned long int)topic);
int x = strlen(mqtt_meta_ctrl_topic);
printf("x = %d\n",x);
// Determine subtopic
const char *subtopic = topic + x;
printf("subtopic = %ld, topic + x = %ld\n", (unsigned long int)(subtopic),\
(unsigned long int)(topic+x));
printf("topic_len - x = %d\n", topic_len - x);
printf("subtopic = '%.*s'", topic_len - x, subtopic);
}
int main(){
HandleIncomingMQTTData(t,(const int)strlen(t),d,(const int)strlen(d));
return(0);
}
输出如下,没有编译器警告或错误:
topic = 'Have a good day !', topic_len = 17
topic = 4196152
x = 13
subtopic = 4196165, topic + x = 4196165
topic_len - x = 4
subtopic = 'ay !'
我觉得问这个问题很愚蠢,因为解决方案一定很明显。我收到以下错误
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
char *subtopic = topic+strlen(mqtt_meta_ctrl_topic);
对于以下代码:
void HandleIncomingMQTTData(const char* topic, const int topic_len, const char* data, const int data_len)
{
// Determine subtopic
char *subtopic = topic+strlen(mqtt_meta_ctrl_topic);
printf("%.*s", topic_len-strlen(mqtt_meta_ctrl_topic), subtopic);
}
如您所见,我尝试在 topic
-string 中获取一个“视图”,其中 subtopic
的地址仍在 topic-string 中,但位于更下游的地址。我想我的指针算法有点不对劲,但我不知道为什么,因为我没有更改 const char *topic
字符串。
topic
是 const char *
,但是你的 subtopic
是 char*
。
const char *subtopic = topic + whatever;
printf("%.*s", topic_len-strlen(...)
请注意 strlen
returns size_t
,但 .*
需要 int
。你应该在这里做一个转换 printf("%.*s", (int)(topic_len - strlen(...))
.
为了提高性能,使用 fwrite
等而不是 printf
可能更好。
查看下面的代码:
#include <stdio.h>
#include <string.h>
char mqtt_meta_ctrl_topic[100] = "Hello world !";
const char *t = "Have a good day !";
const char *d = "R2D2 & C3P0";
void HandleIncomingMQTTData(const char* topic, const int topic_len, \
const char* data, const int data_len)
{
printf("topic = '%s', topic_len = %d\n", topic, topic_len);
printf("topic = %ld\n", (unsigned long int)topic);
int x = strlen(mqtt_meta_ctrl_topic);
printf("x = %d\n",x);
// Determine subtopic
const char *subtopic = topic + x;
printf("subtopic = %ld, topic + x = %ld\n", (unsigned long int)(subtopic),\
(unsigned long int)(topic+x));
printf("topic_len - x = %d\n", topic_len - x);
printf("subtopic = '%.*s'", topic_len - x, subtopic);
}
int main(){
HandleIncomingMQTTData(t,(const int)strlen(t),d,(const int)strlen(d));
return(0);
}
输出如下,没有编译器警告或错误:
topic = 'Have a good day !', topic_len = 17
topic = 4196152
x = 13
subtopic = 4196165, topic + x = 4196165
topic_len - x = 4
subtopic = 'ay !'