user48730: I'm configuring an ESP8266 using Arduino Uno.
I have several MQTT topics I wish it to subscribe, and I do not want to repeat code 3 or 4 times ( in Python I would have created a list - and make a for loop to subscribe all )
These are my topics:
//MQTT topic
const char* deviceName = "Sonoff1";
const char* deviceTopic = "HomePi/Switches/Sonoff1";
const char* msgTopic = "HomePi/Messages";
const char* groupTopic = "HomePi/All";

This is the function registering a single topic:
void reconnect() {
        // Loop until we're reconnected
        while (!client.connected()) {
                Serial.print("Attempting MQTT connection...");
                // Attempt to connect
                if (client.connect(deviceName,user, passw)) {
                        Serial.println("connected");
                        pub_msg("Power On");
                        client.subscribe(deviceTopic);
                } else {
                        Serial.print("failed, rc=");
                        Serial.print(client.state());
                        Serial.println(" try again in 5 seconds");
                        // Wait 5 seconds before retrying
                        delay(5000);
                }
        }
}

Appriciate you kind help,
Guy

user48730: just for a closure:
added an array (thank to Juraj):const char* topicsArry[]={deviceTopic, msgTopic, groupTopic, "HomePi/Dvir/Clock", "HomePi/Dvir/Alerts"};
a for loop to over this that loop, and using sizeof ( which was not trivial for me ), led to :
void reconnect() {
        // Loop until we're reconnected
        while (!client.connected()) {
                Serial.print("Attempting MQTT connection...");
                // Attempt to connect
                if (client.connect(deviceName,user, passw)) {
                        Serial.println("connected");
                        pub_msg("Power On");
                        for (int i=0; i<=sizeof(topicsArry)/sizeof(char *); i++){
                          client.subscribe(topicsArry[i]);
                          char msg[50];
                          sprintf(msg, "Subscribed to %s",topicsArry[i]);
                          pub_msg(msg);
                        }
                } else {
                        Serial.print("failed, rc=");
                        Serial.print(client.state());
                        Serial.println(" try again in 5 seconds");
                        // Wait 5 seconds before retrying
                        delay(5000);
                }
        }
}


user85299: You can make life easier if you arrange your topic names hierarchically.
As an example suppose we have a multimedia controller that accepts various commmands:

mediaserver/set/volume
mediaserver/set/pause
mediaserver/set/audio/source
mediaserver/set/video/subtitles
etc...

You can then subscribe to "mediaserver/set/#"
For looping over the array of topics, you can take advantage of modern C++'s range-based for loop:

https://en.cppreference.com/w/cpp/language/range-for

Example code:
const char *topicArry[] = {
    deviceTopic, msgTopic, groupTopic,
    "HomePi/Dvir/Clock", "HomePi/Dvir/Alerts"
};
void reconnect() {
    // Loop until we're reconnected
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect(deviceName,user, passw)) {
            Serial.println("connected");
            pub_msg("Power On");
            for (const char *topic : topicsArry) {
                client.subscribe(topic);
                char msg[50];
                sprintf(msg, "Subscribed to %s",topic);
                pub_msg(msg);
            }
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}


48730: <p>I'm configuring an ESP8266 using Arduino Uno.</p>

<p>I have several MQTT topics I wish it to subscribe, and I do not want to repeat code 3 or 4 times ( in <code>Python</code> I would have created a list - and make a for loop to subscribe all )</p>

<p>These are my topics:</p>

<pre><code>//MQTT topic
const char* deviceName = "Sonoff1";
const char* deviceTopic = "HomePi/Switches/Sonoff1";
const char* msgTopic = "HomePi/Messages";
const char* groupTopic = "HomePi/All";
</code></pre>

<p>This is the function registering a single topic:</p>

<pre><code>void reconnect() {
        // Loop until we're reconnected
        while (!client.connected()) {
                Serial.print("Attempting MQTT connection...");
                // Attempt to connect
                if (client.connect(deviceName,user, passw)) {
                        Serial.println("connected");
                        pub_msg("Power On");
                        client.subscribe(deviceTopic);
                } else {
                        Serial.print("failed, rc=");
                        Serial.print(client.state());
                        Serial.println(" try again in 5 seconds");
                        // Wait 5 seconds before retrying
                        delay(5000);
                }
        }
}
</code></pre>

<p>Appriciate you kind help,</p>

<p>Guy</p>
 48730: <p>just for a closure:</p>

<p>added an array (thank to Juraj):<code>const char* topicsArry[]={deviceTopic, msgTopic, groupTopic, "HomePi/Dvir/Clock", "HomePi/Dvir/Alerts"};</code></p>

<p>a for loop to over this that loop, and using <code>sizeof</code> ( which was not trivial for me ), led to :</p>

<pre><code>void reconnect() {
        // Loop until we're reconnected
        while (!client.connected()) {
                Serial.print("Attempting MQTT connection...");
                // Attempt to connect
                if (client.connect(deviceName,user, passw)) {
                        Serial.println("connected");
                        pub_msg("Power On");
                        for (int i=0; i&lt;=sizeof(topicsArry)/sizeof(char *); i++){
                          client.subscribe(topicsArry[i]);
                          char msg[50];
                          sprintf(msg, "Subscribed to %s",topicsArry[i]);
                          pub_msg(msg);
                        }
                } else {
                        Serial.print("failed, rc=");
                        Serial.print(client.state());
                        Serial.println(" try again in 5 seconds");
                        // Wait 5 seconds before retrying
                        delay(5000);
                }
        }
}
</code></pre>
 85299: <p>You can make life easier if you arrange your topic names hierarchically.
As an example suppose we have a multimedia controller that accepts various commmands:</p>
<ul>
<li>mediaserver/set/volume</li>
<li>mediaserver/set/pause</li>
<li>mediaserver/set/audio/source</li>
<li>mediaserver/set/video/subtitles</li>
<li>etc...</li>
</ul>
<p>You can then subscribe to &quot;mediaserver/set/#&quot;</p>
<p>For looping over the array of topics, you can take advantage of modern C++'s range-based for loop:</p>
<ul>
<li><a href="https://en.cppreference.com/w/cpp/language/range-for" rel="noreferrer">https://en.cppreference.com/w/cpp/language/range-for</a></li>
</ul>
<p>Example code:</p>
<pre><code>const char *topicArry[] = {
    deviceTopic, msgTopic, groupTopic,
    &quot;HomePi/Dvir/Clock&quot;, &quot;HomePi/Dvir/Alerts&quot;
};
void reconnect() {
    // Loop until we're reconnected
    while (!client.connected()) {
        Serial.print(&quot;Attempting MQTT connection...&quot;);
        // Attempt to connect
        if (client.connect(deviceName,user, passw)) {
            Serial.println(&quot;connected&quot;);
            pub_msg(&quot;Power On&quot;);
            for (const char *topic : topicsArry) {
                client.subscribe(topic);
                char msg[50];
                sprintf(msg, &quot;Subscribed to %s&quot;,topic);
                pub_msg(msg);
            }
        } else {
            Serial.print(&quot;failed, rc=&quot;);
            Serial.print(client.state());
            Serial.println(&quot; try again in 5 seconds&quot;);
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}
</code></pre>
