Passing a pointer to a function

I did search but I cant see what Im doing wrong. I barely understand pointers and even less making use of them. In this case the use of mqtt libraries require it I believe.

Ive stripped down my code to the parts I cant get my head around and causing errors.

As far as I understand whats happening (although probably wrong)
-create a pointer (points to a memory address, not the value itself) and assign a value
Im not sure if the const means the memory address is constant or the content at that address is constant?
-pass the pointer into the function (deferencing? not sure what or why but this is what looking around for help tells me)
-in the function use the & symbol (I think this is how you use the value of the pointer, not the address?)

const char* iotpulsemeter_publish_topic1m = "home/iotpulsemeter/readings/1minute";
const char* iotpulsemeter_publish_topic15m = "home/iotpulsemeter/readings/15minute";
const char* iotpulsemeter_publish_topic1h = "home/iotpulsemeter/readings/1hour";

sendData(*iotpulsemeter_publish_topic1m, meterReading1m, "1m reading = ");
sendData(*iotpulsemeter_publish_topic15m, meterReading15m, "15m reading = ");
sendData(*iotpulsemeter_publish_topic1h, meterReading1h, "1h reading = ");

void sendData(char &publishDestination, double value, String serialData) {
  Serial.print(serialData);
  Serial.println(value);
  client.publish(publishDestination, String(value).c_str());
  pixelSet();
}

the result is

error: binding reference of type 'char&' to 'const char' discards qualifiers

But I dont know why.

Thanks

Untested, but try changing the function definition to

void sendData(const char *publishDestination, double value, const char *serialData) {

and the calls to

sendData(iotpulsemeter_publish_topic1m, meterReading1m, "1m reading = ");
sendData(iotpulsemeter_publish_topic15m, meterReading15m, "15m reading = ");
sendData(iotpulsemeter_publish_topic1h, meterReading1h, "1h reading = ");

If you had included your entire sketch or a minimal reproducible example, instead of a snippet, I'd have been happy to test before replying.

I tried those changes but get the following error.


C:\Users\Steve\Documents\Arduino\IOT_pulse_meter_v8\IOT_pulse_meter_v8.ino:209:16: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
       sendData(iotpulsemeter_publish_topic1m, meterReading1m, "1m reading = ");
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Steve\Documents\Arduino\IOT_pulse_meter_v8\IOT_pulse_meter_v8.ino:241:21: note:   initializing argument 1 of 'void sendData(char*, double, String)'
 void sendData(char *publishDestination, double value, String serialData) {
               ~~~~~~^~~~~~~~~~~~~~~~~~

What confuses me is that inside this function is the publish function for the pubsub library.


void sendData(char *publishDestination, double value, String serialData) {
  Serial.print(serialData);
  Serial.println(value);
  client.publish(publishDestination, String(value).c_str());
  pixelSet();
}

And this takes the pointer without the * character.

I dont understand the error

invalid conversion from 'const char*' to 'char*' [-fpermissive]

I dont see where Im trying to change the value of the const, Im trying to pass the value into the function. Is that even what the error means?

I checked the pubsubclient code and found the publish function defined like this

boolean publish(const char* topic, const char* payload);

So I changed my function to match like this

void sendData(const char* publishDestination, double value, String serialData)

And I get no compilation errors. Can anyone explain why? Pointers is still obviously a bit confusing to me.

I initially create a pointer and assign it a value.
I then pass that into the sendData function. The function defines that argument must be a const char pointer so the value is accepted? or is it saying, that the data at that address matches the requirement of the argument?

'iotpulsemeter_publish_topic1m' is a pointer to a c-string literal. By definition, literals can't be modified at run time, that's why 'iotpulsemeter_publish_topic1m' must be a 'const char *' and not a 'char *'.

But you were trying to pass it to a function whose parameter is a 'char *'. That's a pointer to a character string that is allowed to be modified. Hence, the incompatibility.

Did you try any tutorial from net? The post #3 @Delta_G has also talked about a pointer variable.

I did try to learn a bit more, I know more about pointer now than I did 3 days ago thats for sure.

It looks like I have it sorted now so I will try to implement this and test.

Thanks all

I am presenting a short tutorial for you on "Pointer Variable".

1. In the following declaration, y is an ordinary variable.
byte y = 0x35;

In the above, y is the symbolic name of variable which as been assigned an initial value 0x35.

The value of y resides in a RAM location of the MCU (fig-1). The RAM location has an address. Let us assume that the numerical value of ths address is: 0x0120.
addressRAMLoc
Fogure-1:

2. In the following declation, ptr is called a pointer variable or simply pointer because of appending * symbol at the left-top.
byte *ptr;

In the above, byte is not the data-type of the pointer variable. What does it refer - we will see later?

In a pointer variable, it is possible to store the address of the memory location that holds the value of y in Fig-1. To do it, the following codes are executed:

byte y = 0x35;
byte *ptr;
ptr = &y;     //& (ampersand sign) is used to get the address of RAM location that holds y

Test Sketch:

void setup()
{
  Serial.begin(9600);
  byte y = 0x35;
  byte *ptr;
  ptr = &y;     //& (ampersand sign) is used to get the address of RAM location that holds y
  Serial.println((uint16_t)ptr, HEX);  //shows: 0x08FB
}

void loop() {}

The test sketch has printed 0x08FB for the address of y, which is a valid address as it is within the available RAM space of Fig-1. The value is not fixed; the actual location is determined at the compilation time and is used during run-time (prorgram execution phase).

3. Can pointer variable be used to assign a new value to y?
Yes!
Let us assign 0x45 into variable y of Fig-1.
Test Sketch:

void setup()
{
  Serial.begin(9600);
  byte y = 0x35;
  byte *ptr;
  ptr = &y;     //& (ampersand sign) is used to get the address of RAM location that holds y
  Serial.println(y, HEX); //shows: 35
  //---------------------------
  *ptr = 0x45;  //store 0x45 into a RAM location whose address is in ptr
  Serial.println(y, HEX); //shows: 45
}

void loop() {}

4. Arithmetic operation can be done on pointer variable.
....pending

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.