-1

There is a function that takes the following argument :

int send_message(const char *topic)

I have a struct :

typedef struct mqtt_topic {
    char topic[200];
} mqtt_topic_t;

and a value that is of the type : mqtt_topic_t *mqtt_topic

I am trying to pass mqtt_topic->topic as an argument to the function but it throws an error. How do I convert this data to useful format that I can then use as an argument in my function?

Here is the code snippet :

int mqtt_publish(char message[])
{
    int msg_id = 0;
    ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
    mqtt_topic_t *mqtt_topic = get_mqtt_topic();


    msg_id = esp_mqtt_client_publish(client,&mqtt_topic->topic, message, 0, 1, 0);
    ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
    return msg_id;
}

Function Prototype :

int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic, const char *data, int len, int qos, int retain);
  • 1
    [Arrays decays to pointers](https://stackoverflow.com/q/1461432/12122460), so the code snippets shown in the question doesn't cause compile errors. Please edit the question to include a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – kotatsuyaki Nov 21 '22 at 10:48
  • It should work. Don't you pass `&mqtt_topic->topic` actually? – tstanisl Nov 21 '22 at 10:48
  • Don't define your own types ending in `_t`; in POSIX applications, these are reserved for the implementation, and so defining them causes undefined behavior. – JohnScott Nov 21 '22 at 11:10
  • Added Code snippet – AbheshekSharma Nov 21 '22 at 11:11
  • Please also copy&paste the error message you get when you try to compile exactly the code shown in the question. (You will probably notice that the code cannot be compiled because it is incomplete.) – Bodo Nov 21 '22 at 11:17
  • The code compiles, I get an error log message during run time : E (1043024) MQTT_CLIENT: Publish message cannot be created I (1043028) MQTT_EXAMPLE: sent publish successful, msg_id=-1 TOPIC PUBLISHED : -1I (1043038) BTN: BTN Released. The function returns -1 if message was not sent successfully. – AbheshekSharma Nov 21 '22 at 11:27

2 Answers2

0

The argument &mqtt_topic->topic has type "pointer to char[200]". What you want is pointer to just char:

msg_id = esp_mqtt_client_publish(client, mqtt_topic->topic, message, 0, 1, 0);

When passed as argument, or used in almost any other way except with & and sizeof operators, array decays to pointer to its first element. This is why mqtt_topic->topic gives char*, which is ok as const char* parameter needed here.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • I have tried : msg_id = esp_mqtt_client_publish(client, mqtt_topic->topic, message, 0, 1, 0); But doesnt work. I get the same error log message. Code compiles but the function returns -1, which is unsuccessful. If I replace it with a string like this : msg_id = esp_mqtt_client_publish(client, "example topic", message, 0, 1, 0); Then all goes well. – AbheshekSharma Nov 21 '22 at 11:36
  • @AbheshekSharma Well, then you should probably set `mqtt_topic->topic` to contain the correct string. Or, you have undefined behavior and you see "random" anomalies because of that. – hyde Nov 21 '22 at 11:44
  • @AbheshekSharma Do you know what makes a string to be a string in C? Asking this, because I suspect `mqtt_topic->topic` doesn't actually contain a string. – hyde Nov 21 '22 at 11:45
  • Yes I think you're right, it is not behaving like a string. I can't do any operations on it that I should be able to do on a string, without the code throwing up errors in runtime. – AbheshekSharma Nov 21 '22 at 13:54
  • So you are probably missing the terminating 0-byte. Initialize the array to 0 before using it. Also this might even be a nice, rare use case for `strncpy` (but note how it does not guarantee terminating 0!). – hyde Nov 21 '22 at 14:27
  • You were spot on. I added the following code in another section of the program and it started working : mqtt = (mqtt_topic_t*)malloc(sizeof(mqtt_topic_t)); memset(mqtt, 0x00, sizeof(mqtt_topic_t)); – AbheshekSharma Nov 22 '22 at 03:24
0

you do not need & in front of mqtt_topic->topic.

If your code does not compile (it is giving errors - not warnings) it means that you use a C++ compiler instead of C compiler or you set warning to be treated as errors.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • I have tried : msg_id = esp_mqtt_client_publish(client, mqtt_topic->topic, message, 0, 1, 0); But doesnt work. I get the same error log message. Code compiles but the function returns -1, which is unsuccessful. If I replace it with a string like this : msg_id = esp_mqtt_client_publish(client, "example topic", message, 0, 1, 0); Then all goes well. – AbheshekSharma Nov 21 '22 at 11:36
  • Error log message? – 0___________ Nov 21 '22 at 16:35