Problem with function call


on my photon I want to check in seqence if a connected button was pressed by using this function alls:

89    buttonPressed(BTN_A0, "SparkCoreController/Command", "DOWN");
90    buttonPressed(BTN_A1, "SparkCoreController/Command", "LEFT");
91    buttonPressed(BTN_A2, "SparkCoreController/Command", "UP");

The function looks like this:

108    void buttonPressed(int button, char topic[], char command[]) {
109        // if button has been pressed
110        if(digitalRead(button) == LOW) {
111            client.publish(topic, command);
112       }
113    }

When verifying the code, I get the following error:

89:68: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
client.publish(“SparkCoreController/Init”, “Connected to broker.”);

I am very new to partice and Arduino.
It would be highly appreciated if someone could help me fix this.


The buttonPressed declaration should be:

void buttonPressed(int button, const char topic[], const char command[])

The reason is that string constants (“DOWN”, etc.) are implicitly declared as const char *, which means they cannot be modified (const = constant). The advantage of this is that the strings live only in the flash, and don’t need to be copied into the modifiable RAM. You need to carry this const declaration down into functions that use them, to prevent those functions from allowing modification of the strings.

1 Like