0

I feel stupid for asking this question as the solution must be obvious. I get the following error

error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
     char *subtopic = topic+strlen(mqtt_meta_ctrl_topic);

for the following code:

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);
}

As you can see, I try to get a "view" into the topic-string with subtopic at an address which is still in the topic-string but a little further downstream. I guess my pointer arithmetic is a bit off but I can't figure out why because I don't change the const char *topic string.

glades
  • 3,778
  • 1
  • 12
  • 34
  • 2
    `const` can't be thrown away just because you feel like it. – tadman May 19 '21 at 06:28
  • If you *really* want to remove `const`-ness, I think you can do it in C with `char *subtopic = (char *) (topic+strlen(mqtt_meta_ctrl_topic));` But you will probably get a warning about this too. It doesn't even seem like you need to remove `const`-ness in your function right now. – mediocrevegetable1 May 19 '21 at 06:32
  • I am always confused whether the `char` is const or the pointer is const. Seems here the `char` is const and can't be assigned to a char that may be modified (is not const). – Paul Ogilvie May 19 '21 at 06:33
  • @PaulOgilvie [Classic conundrum](https://stackoverflow.com/questions/890535/what-is-the-difference-between-char-const-and-const-char). – tadman May 19 '21 at 06:34
  • The compiler error is easy enough to read and understand and it points out the exact location of the bug too. What part of "invalid conversion from 'const char*' to 'char*'" is unclear? – Lundin May 19 '21 at 08:25
  • What I don't understand is why i can't use the const topic string as a const as I don't intend to change it. I thought that the conversion would be implicit. Also, it doesn't make sense to cast away the const-ness from a contextual point of view. It seems like a dirty hack. – glades May 19 '21 at 13:05

2 Answers2

1

topic is const char *, but your subtopic is char*.

 const char *subtopic = topic + whatever;
printf("%.*s", topic_len-strlen(...)

Note that strlen returns a size_t, but .* expects an int. You should do a cast here printf("%.*s", (int)(topic_len - strlen(...)).

It might be better to use fwrite or such, instead of printf, for performance.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Indeed. The problem was that I didn't realize that IF subtopic was only char (without const), then I could change the memory of the topic variable with a simple *subtopic = '\0' or similar, which would circumvent the idea of const-ness :) – glades May 20 '21 at 12:23
-3

See the code below:

#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);
}

Output is as follows, there are no compiler warnings or errors:

topic = 'Have a good day !', topic_len = 17
topic = 4196152
x = 13
subtopic = 4196165, topic + x = 4196165
topic_len - x = 4
subtopic = 'ay !'
Dharman
  • 30,962
  • 25
  • 85
  • 135