2

topicArray defined as follows:

const char* msgTopic = "Home/Messages";
const char* groupTopic = "Home/All";
char* deviceName = deviceTopic;
const char* topicArry[2] = {deviceTopic, groupTopic};

But deviceTopic can be changed during code run, and I wish that topicArray containg deviceTopic will have the correct value after deviceTopic has changed.

What is the right way doing so ?

5
  • 1
    don't make the array 'constant' with const and you can change the values (pointers) it contains
    – Juraj
    Commented Jan 11, 2019 at 20:00
  • @Juraj I did remove const ouf of array and all other variable, but still array is not changed
    – guyd
    Commented Jan 11, 2019 at 20:03
  • do you change it?
    – Juraj
    Commented Jan 11, 2019 at 20:23
  • as said " I did remove const"
    – guyd
    Commented Jan 11, 2019 at 20:24
  • to be able to set the value topicArray[0] = "something"
    – Juraj
    Commented Jan 11, 2019 at 20:26

1 Answer 1

2

You may try something like this:

const char* topicArry[2] = {NULL, groupTopic};
const char** const pDeviceTopic = &topicArry[0];
#define deviceTopic (*pDeviceTopic)

Arguably not very elegant, but now when you assign, e.g.

deviceTopic = msgTopic;

you are actually writing into topicArry[0].

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