1

I have a program that does 4 times the same thing with different parapmeters; and was trying to simplify my code to take one parameter as a variable.

Example (what was):

const char MQTT_PUB_IRRIGATION_TANK_VALVE[]      = "ArgyleCourt/Property/Hub/IrrigationTank/Valve";
const char MQTT_PUB_HOUSE_BOREWATER_TANK_VALVE[] = "ArgyleCourt/Property/Hub/HouseBoreWaterTank/Valve";

publishTankValveStatusIrrigation(false);

void publishTankValveStatusIrrigation(bool demandStatus) {
  if (demandStatus) {
    mqttClient.publish(MQTT_PUB_IRRIGATION_TANK_VALVE, "ON");
  } else {
    mqttClient.publish(MQTT_PUB_IRRIGATION_TANK_VALVE, "OFF");
  }
}

publishTankValveStatusHouse(false);

void publishTankValveStatusHouse(bool demandStatus) {
  if (demandStatus) {
    mqttClient.publish(MQTT_PUB_HOUSE_BOREWATER_TANK_VALVE, "ON");
  } else {
    mqttClient.publish(MQTT_PUB_HOUSE_BOREWATER_TANK_VALVE, "OFF");
  }
}

to one function now passing the topic also inot the function, thus needing only one function:

publishTankValveStatus(false, MQTT_PUB_IRRIGATION_TANK_VALVE);
publishTankValveStatus(false, MQTT_PUB_HOUSE_BOREWATER_TANK_VALVE);

void publishTankValveStatus(bool demandStatus, char topicToPublishTo) {
  if (demandStatus) {
    mqttClient.publish(topicToPublishTo, "ON");
  } else {
    mqttClient.publish(topicToPublishTo, "OFF");
  }
}

when I compile the simplified function, I get "warning: invalid conversion from 'const char*' to 'char' [-fpermissive]", but do neither understand why or how to fix it.

1

The function declaration

void publishTankValveStatus(bool demandStatus, char topicToPublishTo)

declares topicToPublishTo as a char, that is, as a single character instead of as an array of characters that is constant.

Change char to const char * to say that topicToPublishTo is an array of characters that is constant.

  • Thanks! Works :) You wrote const char *; is it permissible or good practice or even the same to write const char* ? This C business with single char, many char, and pointer really trips me all the time :( – MaxG Apr 22 '17 at 4:03
  • @MaxG, both forms are ok; which to use is mostly personal preference. I think const char*, with the star close to char, is used much more commonly than with a space between. But if you are declaring several variables on one line (which some may frown on) it makes sense to associate the star with the variable name. Eg, in char *a, b, *c, d, e;, only a and c are pointers vs. single characters. Also, for the parameter declaration, some would say const char topicToPublishTo[] instead of const char *topicToPublishTo – James Waldby - jwpat7 Apr 22 '17 at 4:26
  • Also see answers to const char * const versus const char *?, which cover examples like char* astring, const char* astring, char* const astring, and const char* const astring. Note, answers to What is the difference between char * const and const char *? might be more understandable. – James Waldby - jwpat7 Apr 22 '17 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.