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 Answer 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 writeconst char*
? This C business with single char, many char, and pointer really trips me all the time :(MaxG– MaxG2017年04月22日 04:03:21 +00:00Commented Apr 22, 2017 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, inchar *a, b, *c, d, e;
, onlya
andc
are pointers vs. single characters. Also, for the parameter declaration, some would sayconst char topicToPublishTo[]
instead ofconst char *topicToPublishTo
James Waldby - jwpat7– James Waldby - jwpat72017年04月22日 04:26:21 +00:00Commented Apr 22, 2017 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
, andconst char* const astring
. Note, answers to What is the difference between char * const and const char *? might be more understandable.James Waldby - jwpat7– James Waldby - jwpat72017年04月22日 04:27:40 +00:00Commented Apr 22, 2017 at 4:27